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.

So here is my query:

$args = array( 
    'post_type' => 'Event',
    'posts_per_page' => 1000,
    'meta_key' => 'event_informations_show_on_the_homepage',
    'meta_value' => 'Show on the homepage',
    'meta_compare' => '==',
    'meta_key' => 'event_informations_date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
);

$loop = new WP_Query( $args );

I want to select all posts that have the Custom field event_informations_show_on_the_homepage and the value of the Custom field event_informations_show_on_the_homepage and order by the date Custom field which is stored as a timestamp and is called event_informations_date.

What am I doing wrong?

share|improve this question
    
isn't this a repeat of: wordpress.stackexchange.com/questions/43277/… –  helgatheviking Feb 23 '12 at 19:18
add comment

1 Answer

up vote 1 down vote accepted

@Uffo I would say your args are wrong, should be something like:

$args = array(
    'post_type' => 'Event',
    'posts_per_page' => 1000,
    'meta_query'=> array(
        array(
            'key'=>'event_informations_show_on_the_homepage',
            'value'=> 'Show on the homepage',
            'compare' => '=='
        )
    ),
    'meta_key'=>'event_informations_date',
    'orderby'=>'meta_value_num', 
    'order'=>'ASC'
);

The way you did it, it's just the last meta_key that will be used both for filtering the query and to order. I'm not 100% sure if this way will work but give it a try.

Hope I've helped.


So I think you will need to filter the Group By SQL statement to make it work the way you want, something like that:

$args = array(
    'post_type' => 'Event',
    'posts_per_page' => 1000,
    'meta_query'=> array(
        'relation' => 'OR',
        array(
            'key'=>'event_informations_show_on_the_homepage',
            'value'=> 'Show on the homepage',
            'compare' => '=='
        ),
        array(
            'key'=>'event_informations_date',
        )
    ),
    'orderby'=>'meta_value_num', 
    'order'=>'ASC'
);

add_filter('posts_orderby', 'edit_posts_orderby');

function edit_posts_orderby($orderby_statement) {
    $orderby_statement = "mt1.meta_value+0 ASC";
    return $orderby_statement;
}
share|improve this answer
    
Well it works Sir, but is not ordering them by the event_informations_date which is stored as unixtime.. –  Uffo Feb 23 '12 at 20:24
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.