the_query : add 'NULL'/'NOT EXISTS' custom field
-
Hello,
I want to display all the posts with the custom field ‘status’ set to ‘0’ OR custom field ‘status’ not set at all (NULL or NOT EXISTS?). But I’m not really sure of the syntax.
The following code doesn’t work: it seems to display all articles, even those with the field ‘status’ set to ‘1’ or ‘2’ for example.
$args = array( 'posts_per_page' => -1, 'post_status' => 'pending', 'relation' => 'OR', array( 'meta_key' => 'status', 'meta_value' => '0' ), array( 'meta_key' => 'status', 'meta_compare' => 'NOT EXISTS' ) ); $the_query = new WP_Query( $args ); if( $the_query->have_posts() ): ?> <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?> <?php echo get_the_title(); ?> <?php endwhile; ?> <?php endif; ?> <?php wp_reset_query();
Thanks for helping!
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘the_query : add 'NULL'/'NOT EXISTS' custom field’ is closed to new replies.