Support

Account

Home Forums General Issues Get posts by ACF if not exists Reply To: Get posts by ACF if not exists

  • you’ve specified a meta key, but not a value

    
    $new_args = [
    
        'post_type'      => 'news',
        'post_status'    => 'publish',
        'fields'         => 'ids',
        'posts_per_page' => '5',
        'meta_key'       => 'is_featured',
        'meta_value'     => '1',
        'orderby'        => 'meta_value_num', // not sure that this does
        'order'          => 'desc'
    ];  
    $news = new WP_Query($new_args);
    

    Either that or you need to use a meta_query parameter. https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters. You need to test for the value and you need to test that the value exists. See the “compare” argument.

    
      'meta_query' => array(
        array(
          'key' => 'is_featured',
          'compare' => 'EXISTS'
        ),
        array(
          'key' => 'is_featured',
          'value' => '1'
        ),
      ),