Reputation: 2027
Why does this return all posts;
$partner = get_query_var('partner');
echo "Partner ID: ". $partner; // Echoes correctly
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
'meta_key' => 'partner',
'meta_value' => array($partner), // also $partner (without array)
'compare' => 'LIKE', // Also 'IN'
),
);
$partner_query = new WP_Query($args);
the ACF field 'partner' is an array, the query variable is a string (obv)
Upvotes: 1
Views: 1058
Reputation: 2027
For anyone who gets stuck in this rabbit hole.
My query parameter is a string, ACF stores the IDs as a string if there's one and an array if there's more than one.
This form of the arguments returns the correct results. (many thanks to Howard E and IronCanTaco)
$partner = get_query_var('partner'); // string
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'partner', // Could be string or array
'value' => $partner,
'compare' => 'LIKE',
),
),
);
Upvotes: 0
Reputation: 5639
The correct usage of meta_query
is like this:
The args shouldn't be meta_key
if you're using meta_query - they should be key
, value
, etc.
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'partner',
'value' => array( $partner ),
'compare' => 'IN',
),
),
);
Upvotes: 2
Reputation: 111
Try like this:
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
'meta_key' => 'partner',
'meta_value' => array($partner), // also $partner (without array)
'compare' => '=', // Also 'IN'
),
);
$partner_query = new WP_Query($args);
Upvotes: 1