Search code examples
phpwordpresscustom-fieldsmeta-key

WordPress: WP_Query with posts from a category and meta_query or only another meta_query


I have some trouble with WordPress and the WP_Query. I would like to get posts filtered by meta_query and/or category, but I have the following problem: The first type of posts has a custom field named "type" which must be filled with "exercise" and the post has to be in a category named "Level" (this will be set before). The second type of posts has only the custom field named "type" which must be filled with "test".

I don't know how to get these two conditions together. Because of that I've tried to split it into two Queries, and merge it afterward, like this:

$firstArgs = array(
        'posts_per_page'=> -1,
        'category_name' => $level,
        'meta_key'      => 'duration',
        'orderby'       => 'meta_value_num',
        'order'         => 'DESC',
        'meta_query' => array(
             'key' => 'type',
             'value' => 'exercise'
        )     
);

$secondArgs = array(
        'posts_per_page' => -1,
        'meta_key'       => 'duration',
        'orderby'        => 'meta_value_num',
        'order'          => 'DESC',
        'meta_query' => array(
             'key' => 'type',
             'value' => 'test'
        )
);
$first_query = new WP_Query( $firstArgs );
$second_query = new WP_Query( $secondArgs );
$result = new WP_Query();
$result->posts = array_merge($first_query->posts, $second_query->posts);

The Problem with this method is, that I would like to sort the posts by the custom field "duration" DESC. If I merge these two arrays, the sorting isn't that way I would like to.

Does anyone know a better way of doing this? If it would be one query, the sorting would work and I think it is more efficient.

Thanks for your help!


Solution

  • I found an answer that worked for me. First of all, I retrieve the data as Dhruv told me and then, I sorted the result with the usort-function, but with an anonymous function.

    usort($your_posts, function($a, $b)
    {
          $val1 = get_post_meta($a->ID, 'duration', true );
          $val2 = get_post_meta($b->ID, 'duration', true );
          return  -1*($val1 - $val2); // sort desc
    });
    

    Maybe it is not the best method, but it worked for me. :-)