0

I need to search post and custom post type which are both in the same category, but I need to filter only post type by a meta with a custom field. Using meta_query I tried

$meta_query =   array(array(
      'key'      => 'public',
      'value' => '1',
      'type'  => 'NUMERIC',
      'compare' => '='    
    )
); 

but simple post doesn't have this meta so they are excluded. How can I refer this meta query only to custom post type and not to post?

1

Solved. I needed to change meta_query this way:

$meta_query = array(
    'relation' => 'OR',
    array(
        'key'   => 'public',
        'value' => '1',
    ), 
    array(
        'key'     => 'public',
        'value'   => '',  // necessary to work!!!
        'compare' => 'NOT EXISTS'
    ), 
);

1st array search for post with meta key 'public' set to 1

2nd array add post without meta key 'public'

note that in 2nd array it's necessary to add value also if useless!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.