Custom Field Query – Meta Value is Array

I’m using a custom field query (or trying to) :

$args =  array(
    'post_type'     => 'pf_cookbook', 
    'meta_query'    => array(
        'key'           => 'pf_cookbook_recipes',
        'value'         => '5',
        'compare'       => 'NOT IN',
        'type'          => 'NUMERIC'
    ) 
);

However, the meta value to be compared is an array. The idea here is I am querying the DB to get the cookbooks which don’t contain a certain recipe. The Recipes & Cookbooks are Custom Post Types. The Cookbooks have a meta key ‘pf_cookbook_recipes’ which I am storing an array of recipe IDs.

Read More

Maybe I am missing something?

Related posts

Leave a Reply

1 comment

  1. meta_query needs to be an array of arrays – have a look at the code sample in the Codex again.

    So, for your example:

    $args =  array(
       'post_type'   => 'pf_cookbook', 
       'meta_query'  => array(
                           array(
                            'key'           => 'pf_cookbook_recipes',
                            'value'         => '5',
                            'compare'       => 'NOT IN',
                            'type'          => 'NUMERIC'
                            )
                        )
    ); 
    

    );