Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer
    
Thanks!!! Just been as you say: D Awesome! –  Guiloviu Jul 8 '13 at 18:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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