• Hi Guys,

    I think I have a somewhat unique WP Query issue (maybe I’m wrong, but can’t seem to find too many references to it) and I would appreciate any help with this. I have a custom type with a custom field that doesn’t auto create (I don’t want contributors to see it just editors) when a post is created, but when it is created I want the query to filter it out. I’m using this field to “assign” posts into certain areas on certain pages. On the home page I want to show 3 slots where articles are manually assigned and then show the rest of the articles that either don’t have that field or if they do don’t have any of the assignable values (I hope this makes sense). Below is my query, but it will only return items that have the custom field and won’t return articles that don’t have that field.

    Any help or suggestions would be greatly appreciated!

    if (is_front_page()){
    $args = array(
    ‘meta_key’=>’frontpage_slot’,
    ‘orderby’ => ‘meta_value_num’,
    ‘meta_key’ => ‘frontpage_slot’,
    ‘meta_query’ => array(
    ‘relation’ => ‘OR’,
    array(
    ‘key’ => ‘frontpage_slot’,
    ‘compare’ => ‘NOT EXISTS’,
    ‘value’ => ”,
    ),
    array(
    ‘key’ => ‘frontpage_slot’,
    ‘value’ => NULL,
    ‘compare’ => ‘=’,
    ),
    array(
    ‘relation’ => ‘AND’,
    array(
    ‘key’ => ‘frontpage_slot’,
    ‘value’ => 1,
    ‘compare’ => ‘!=’,
    ),
    array(
    ‘key’ => ‘frontpage_slot’,
    ‘value’ => 2,
    ‘compare’ => ‘!=’,
    ),
    array(
    ‘key’ => ‘frontpage_slot’,
    ‘value’ => 3,
    ‘compare’ => ‘!=’,
    ),
    ),
    ),
    ‘post_type’=> ‘article’,
    ‘showposts’=> 5,
    ‘order’=>’DESC’
    );
    $nopage=1;
    }

    Thanks,
    Ali

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello, aaoudi2 !

    Try

    array(
    'key' => 'frontpage_slot',
    'compare' => 'NOT EXISTS',
    'value' => '',
    ),
    array(
    'key' => 'frontpage_slot',
    'compare' => '='
    'value' => '',
    ),
    Thread Starter aaoudi2

    (@aaoudi2)

    Thanks DenUSenko, but unfortunately it doesn’t change the result and returns only posts that have that metakey and ignores ones that are missing it.

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

The topic ‘WP Query Filter with Custom Post Field’ is closed to new replies.