Support » Developing with WordPress » Query not returning posts with null custom fields

  • Resolved nicemodernwebsites

    (@nicemodernwebsites)


    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:

    // 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:

    $meta_query = array(
        array(
          'key' => 'is_column',
          'value' => '1',
          'compare' => '!='
        )
      );

    or even like this:

    $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 to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello, based on the WP_Query docs, please try the following:

    // 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 18 hours, 26 minutes ago by Gerry. Reason: Accidentally submitted reply
    Thread Starter nicemodernwebsites

    (@nicemodernwebsites)

    We’re golden. Thanks Gerry!

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.