0
votes

Well, I've a created a post type with several custom fields (with ACF) and now I need to retrive the custom post_type by one of it's custom fields, that has an array as the meta_value, this is the code that I'm using to get the custom post_type

$args = array(
    'post_type'  => 'featured',
    'posts_per_page'    => 1,
    'post_status'     => 'publish',
    'meta_query' => array(
        array(
            'key'     => 'where_it_will_show',
            'value'   => 156,
            'compare' => 'IN',
        ),
    ),
);
$where = new WP_Query($args);

If I remove the media_query part it works, but I need this filter :/ If anything wrong with my code? Is there another option to get this filter done?

Thanks in advance

1
What kind of field is where_it_will_show?Niels van Renselaar
@NielsvanRenselaar it's an ACF Relation field that relates where the feature post_type will show, example: feature post_type will show in home, about us and a post, I've configured it to gets the ID of each relationed postAyesha Lomaski
As far as I know it stores the data as a serialized array... have you looked at the database what the stored value is? In that case an IN query will not work.Niels van Renselaar

1 Answers

0
votes

Thanks to Niels van Renselaar I've discovered that I was using the wrong "compare" value, the right code is as below:

$args = array(
'post_type'  => 'featured',
'posts_per_page'    => 1,
'post_status'     => 'publish',
'meta_query' => array(
    array(
        'key'     => 'where_it_will_show',
        'value'   => '"156"',
        'compare' => 'LIKE',
    ),
),

); $where = new WP_Query($args);