Title: Query not returning posts with null custom fields
Last modified: February 5, 2024

---

# Query not returning posts with null custom fields

 *  Resolved [nicemodernwebsites](https://wordpress.org/support/users/nicemodernwebsites/)
 * (@nicemodernwebsites)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/query-not-returning-posts-with-null-custom-fields/)
 * I have wordpress site with a few hundred posts, all categorized and tagged for
   years. I am splitting them up into “news” and “columns”, and have created a new
   true/false field, “is_column”; I want to run a query to display posts with is_column
   = 1 in one area of the front page, and posts with is_column as null to display
   in another.
 * one half — displaying columns — works fine:
 *     ```wp-block-code
       // Create a meta query to filter posts by the custom field "is_column"
   
       $meta_query = array(
           array(
             'key' => 'is_column',
             'value' => '1',
             'compare' => '=='
           )
         );
         // Create an array of arguments
         $args = array(
           'post_type' => 'post', // Post type
           'posts_per_page' => 4, // Number of posts to return
           'meta_query' => $meta_query, // The meta query created above
           'orderby' => 'post_date', // Order by post date
           'order' => 'DESC', // Order from newest to oldest
           'ignore_sticky_posts' => false // Include sticky posts at the top
         );
         // Create a new WP_Query object with the arguments
         $query = new WP_Query($args);
         // Check if the query has any posts
         if ($query->have_posts()) {
           // Loop through the posts
           while ($query->have_posts()) {
             // Set up the post data
             $query->the_post();
             // Display the post title
             echo '<a href="'. get_the_permalink() .'">';
             the_title(); // Change this from the_content() to the_title()
             echo '</a>';
           }
           // Restore the original post data
           wp_reset_postdata();
         } else {
           // No posts found
           echo 'No posts found with the custom field "is_column" set to true.';
         }
       ```
   
 * However, when I invert the query like this:
 *     ```wp-block-code
       $meta_query = array(
           array(
             'key' => 'is_column',
             'value' => '1',
             'compare' => '!='
           )
         );
       ```
   
 * or even like this:
 *     ```wp-block-code
       $meta_query = array(
           array(
             'key' => 'is_column',
             'value' => NULL,
             'compare' => '=='
           )
         );
       ```
   
 * …it falls back to ‘No posts found’.
   How can I run this query?
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fquery-not-returning-posts-with-null-custom-fields%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Gerry](https://wordpress.org/support/users/metamezzo/)
 * (@metamezzo)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/query-not-returning-posts-with-null-custom-fields/#post-17403044)
 * Hello, based on the [WP_Query docs](https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters),
   please try the following:
 *     ```wp-block-code
       // for the first column
       $meta_query = array(
           array(
             'key'     => 'is_column',
             'value'   => '1',
             'compare' => '=',
           ),
       );
   
       // for the second column, the inverse...
       $meta_query = array(
           array(
             'key'     => 'is_column',
             'compare' => 'NOT EXISTS',
           ),
        );
       ```
   
 * Good luck!
    -  This reply was modified 2 years, 4 months ago by [Gerry](https://wordpress.org/support/users/metamezzo/).
      Reason: Accidentally submitted reply
 *  Thread Starter [nicemodernwebsites](https://wordpress.org/support/users/nicemodernwebsites/)
 * (@nicemodernwebsites)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/query-not-returning-posts-with-null-custom-fields/#post-17403161)
 * We’re golden. Thanks Gerry!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Query not returning posts with null custom fields’ is closed to new replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 2 participants
 * Last reply from: [nicemodernwebsites](https://wordpress.org/support/users/nicemodernwebsites/)
 * Last activity: [2 years, 4 months ago](https://wordpress.org/support/topic/query-not-returning-posts-with-null-custom-fields/#post-17403161)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
