Support

Account

Home Forums ACF PRO Querying against a newly-added field

Solved

Querying against a newly-added field

  • I just added a new field to a field group that I created a while ago. The new field is a true/false field called leave_this_article_out_of_similar_articles, and I’d like to execute a WP_Query() that excludes any post that has the new true/false field checked (true). The problem is, almost all my posts don’t have the new true/false field in the meta data.

    Is there a way to write the query that ignores older posts without the leave_this_article_out_of_similar_articles field, as if they did have the field and it was false?

    This is my code:

    $first_args = array (
    ‘post_type’ => ‘post’,
    ‘post_status’ => ‘publish’,
    ‘tag__and’ => $tag_list,
    ‘orderby’ => ‘modified’,
    ‘order’ => ‘DESC’,
    ‘posts_per_page’ => 10,
    ‘fields’ => ‘ids’,
    ‘meta_query’ => array ( // advanced custom field value
    ‘relation’ => ‘AND’,
    array (
    ‘key’ => ‘leave_this_article_out_of_similar_articles’,
    ‘value’ => ‘1’,
    ‘compare’ => ‘!=’
    )
    )
    );

    $first_posts = new WP_Query( $first_args );

  • 
    'meta_query' => array(
      'relation' => 'OR',
      array(
        'key' => 'leave_this_article_out_of_similar_articles',
        'value' => '1',
        'compare' => '!='
      ),
      array(
        'key' => 'leave_this_article_out_of_similar_articles',
        'compare' => 'NOT EXISTS’'
      )
    ) 
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.