0

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?

1
1

@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;
}
1
  • 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

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.