Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress: Exclude posts with meta_query - Not every posts has the meta_field

I want to exclude every post with a specific value of a custom meta field. The problem is, that not every posts has this meta field.

My code looks like this (excerpt of the working loop):

// WP_Query arguments
        $args = array (
            'post_parent'   => $parentid,
            'orderby'       => 'menu_order',
            'order'         => 'ASC',
            'post_type'     => array( 'page' ),
            'meta_query' => array(
                array(
                    'key' => 'hide',
                    'value' => 1,
                    'compare' => '!='
                )
            )
        );

Not every posts uses the field "hide". Some posts giving back a NULL. So I think, the loop isn't working because of that?!

Is this correct? Is it necessary that every posts has a value for that key?

like image 679
Cray Avatar asked Jan 07 '16 18:01

Cray


Video Answer


1 Answers

Another way to do it:

// WP_Query arguments
    $args = array (
    'post_parent'   => $parentid,
    'orderby'       => 'menu_order',
    'order'         => 'ASC',
    'post_type'     => array( 'page' ),
    'meta_query' => array('0' => array('key' => 'hide', 'value' => '1', 'compare' => 'NOT EXISTS')
    )
);
like image 117
bpy Avatar answered Sep 20 '22 22:09

bpy