I have created a custom post type called 'events'. On the index page, I want to amend the loop to list all posts (standard and events). However, I want to order standard posts by the date it was posted, and I want to order events by a custom field which contains the date of the event.


Standard Post  (Posted on 12th May 2014)
Standard Post  (Posted on 11th May 2014)
Event Post     (10th May 2014)
Standard Post  (Posted on 9th May 2014)
Event Post     (8th May 2014)
Event Post     (7th May 2014)
Standard Post  (Posted on 6th May 2014)

I have added these parameters for query_post on the index.php

  query_posts( array( 
    'post_type'   => array( 'events', 'post'),
    'meta_key'    => 'date_of_event',
    'orderby'     => 'meta_value',
    'order'       => 'asc',
    'meta_query'  => array(
        'key'     => 'date_of_event',
        'value'   => date("Ymd"),
        'compare' => '>=',          
        'type'    => 'DATE'         

At the moment I only have events being returned

1 Answer 1


You're only referencing the event date in your query, which is why you're not getting normal posts (because I assume they don't have an event date). You can use "orderby" with multiple values (separated by a space), so you should be able to change it to:

'orderby'     => 'meta_value date'

That should mean that the query falls back to your post date if there's no event date, for regular posts.

  • Cheers Pete, but someone pointed out that if it doesn't match the meta_key that it wont be returned. It looks like I might need to do something a bit more complex to accomplish what I need
    – CraigColes
    Commented May 14, 2014 at 13:30
  • You could always "cheat" and have event dates assigned to posts, but hidden. Obviously I don't know what other fields you're using on your custom post type, but does it warrant a whole other type? If it's viable to just use "Posts" with a category for Event, your optional fields (like Event Date) could show / hide on the post edit page depending on the category selected. That way a value is present even on non Events (though empty), so sorting should work.
    – Pete Gale
    Commented May 14, 2014 at 15:32

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.