I have a custom post type with a relationship custom field. I try to do a query based on this custom field using the comparator IN and some IDs in an array, but so far with no success. Here is my code:
$myArgs[] = array(
‘key’ => ‘custom_field’,
‘compare’ => ‘IN’,
‘value’ => array(‘100126′,’100135’)
);
$myQuery = new WP_QUERY(array(
‘post_type’ => ‘my_custom_post_type’,
‘posts_per_page’ => -1,
‘meta_query’ => $myArgs
));
What I don’t understand is that a query using ‘LIKE’ works fine:
$myArgs[] = array(
‘key’ => ‘custom_field’,
‘compare’ => ‘LIKE’,
‘value’ => ‘100126’
);
But as soon as I use ‘IN’ and an array for values, no result.
I think it is related to the fact that the data are serialized (ie: a:3:{i:0;s:6:”100126″;i:1;s:6:”100140″;i:2;s:6:”100144″;} ) but I don’t know what to do next.
Hope someone can help, thank you!
You cannot to an “IN” query on a relationship field.
see section 3 of this doc https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
You need to do
$meta_query = array(
'relation' => 'OR',
array(
'key' => 'field_name',
'value' => '"'.$first_id.'"',
'compare' => 'LIKE'
),
array(
'key' => 'field_name',
'value' => '"'.$second_id.'"',
'compare' => 'LIKE'
),
// etc..
);
I understand John thank you for your help and the link
How can I create a meta_query with an array as meta_field?