0

After performing this question, I went to the next step and I wanted to sort the query results by another custom field.

I want to sort the results by a custom field that consists of a date and want to get the order from lowest to highest date

The problem is that I do not get what I ordered. I've tried several ways with no success, I googled several sites and nothing. It gives me much trouble to ask you again but I can not solve this problem.

Anyone can help me? : '(

I leave my code to see where you should embed the orderby:

    $group_1 = get_posts(
    array(
        'posts_per_page' => 30,
        'fields'         => 'ids',
        'meta_query'     => array (
            array (
                'key'   => 'field1',
                'value' => 'data1,
            ),
            array (
                'key'   => 'field2',
                'value' => 'data2',
            ),
        ),
    )
);

$group_2 = get_posts(
    array(
        'posts_per_page' => 30,
        'fields'         => 'ids',
        'meta_query'     => array (
            array (
                'key'   => 'field1',
                'value' => 'data2',
            ),
            array (
                'key'   => 'field2',
                'value' => 'data1',
            ),
        ),
    )
);

$the_query = new WP_Query(
    array(
        'post__in' => array_merge( $group_1, $group_2)
    )
);

Thanks!

1 Answer 1

0

You need 'order_by' => 'post__in' in the last query in order to "Preserve post ID order given in the post__in array (available with Version 3.5).".

I don't know if the order you are passing in is the order you want after the array_merge( $group_1, $group_2). In other words, you need to feed that second query an ordered set of IDs and you don't seem to be doing that.

I also can't tell if one or the meta_keys you've already queried is what you want to orderby. Possibly something like this will work:

$the_query = new WP_Query(
    array(
        'post__in' => array_merge( $group_1, $group_2),
        'meta_key' => 'your_date_field_key',
        'orderby'  => 'meta_value',
    )
);

That is completely untested. Possibly buggy. Caveat emptor. No refunds.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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