0

I am attempting to return posts for a specific query. Using the following code:

$meta_query = array(
    'relation' => 'OR',
    array(
        'key'     => 'tic_assignment',
        'value'   => $current_user -> ID,
        'compare' => '=',
    ),
    array(
        'key'     => 'tic_department',
        'value'   => $_depts,
        'compare' => 'LIKE',
    ),
);
$_args = array ( 'post_type' => 'tickets',
    'meta_query' => $meta_query,
    'posts_per_page' => 10, 
    'orderby' => 'date', 
    'order' => 'DESC', );
$the_query = new WP_Query( $_args );

Now, instead of returning my posts based on the query added, it still returns every post.

I have var_dumped $the_query and what is returned is here: https://pastebin.com/PPYaJpSu

(It's just too big to paste it all here)

Even if I take out the second array, it still returns everything... when it should not return anything based on the tic_assignment

When I modify the arguments to:

$_args = array ( 'post_type' => 'tickets',
    'meta_key' => 'tic_assignment',
    'meta_value' => $current_user -> ID,
    'meta_compare' => '=',
    'posts_per_page' => 10, 
    'orderby' => 'date', 
    'order' => 'DESC', );
$the_query = new WP_Query( $_args );

The proper query is constructed, and the proper results are returned.

What gives?

  • NOTE: $current_user -> ID and $_depts are populated properly, and are not throwing any kind of error – Kevin Dec 14 '17 at 18:06
0

You forgot the meta_query key in your $args.

$meta_query = array(
    'relation' => 'OR',
    array(
        'key'     => 'tic_assignment',
        'value'   => $current_user -> ID,
        'compare' => '=',
    ),
    array(
        'key'     => 'tic_department',
        'value'   => $_depts,
        'compare' => 'LIKE',
    ),
);
$_args = array ( 'post_type' => 'tickets',
    'meta_query' => $meta_query,
    'posts_per_page' => 10, 
    'orderby' => 'date', 
    'order' => 'DESC', );
$the_query = new WP_Query( $_args );
  • I thought that as well, and added it right after I posted the question. No difference – Kevin Dec 14 '17 at 18:04
  • Can you show how you're showing results ? Are you using the $the_query->have_posts() ? – JR WP Dec 14 '17 at 18:15
  • Yes. Besides that, as shown in the pastebin, I am var_dumping $the_query, and the query in it shows the meta_query is NOT getting added to it, when using the meta_query. It does, however, get modified if I use just meta_key, meta_value – Kevin Dec 14 '17 at 18:34
  • if worse comes to worse, i can always dump to: $wpdb->get_results and write the custom query I need for this, was just really hoping to not have to go that route ;) – Kevin Dec 14 '17 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.