My custom 'Home' page is set to sort my custom posts via the custom date field with this (updated to be custom named) code in functions.php:

// sort order for home page
add_action('wp', 'wwgo_check_page');
function wwgo_check_page () {
if (is_page()) {
    add_filter('get_previous_post_sort', 'sort_it');
    add_filter('get_next_post_sort', 'sort_it');
    add_filter('posts_orderby', 'sort_it' );
    add_filter('posts_join', 'join_it' );
    add_filter('posts_where', 'where_it' );

    function sort_it () {
        global $wpdb;

        return " $wpdb->postmeta.meta_value ASC ";

    function join_it( $join ) {
        global $wpdb;

        $join .= " LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id) ";

      return $join;

    function where_it( $where ) {
        global $wpdb;
        $where .= "AND $wpdb->postmeta.meta_key = 'deadline' ";

      return $where;


Which is dandy and simple. Now. I want this to work across the whole site. I want posts on archive pages, categories etc to be sorted first by the custom date field.

Any ideas?

I have tried changing the 'if' to is_archive() but that had no effect. I tried <! is_single()> too.

I'm sure it should be something simple. But I'm new to php and thus clueless.

Here is the site, where you can see the home page is nicely sorted, but if you click 'poetry' (the only one with a few posts), sorting doesn't work.

Any suggestions will be met with much joy.

Thank You Kindly.

  • You should prefix all your function with for eg. josh_ to avoid naming colisions.
    – kaiser
    Commented Jun 21, 2011 at 2:40
  • Do you mean that as good practice or to solve this? I tried copying the code in again for 'is_archive' and it did let me call the function again, but sorting didn't work...
    – josh
    Commented Jun 21, 2011 at 9:31
  • 1
    Yes it's good practice. Plus: You shouldn't wrap functions in functions.
    – kaiser
    Commented Jun 21, 2011 at 15:18
  • cool, cool (i just copied the code from a website, tbh. I'm new to php & wordpress). Any advance on how to get this to work across the site? I have it working with more custom pages and the same function set to (is_page()) but still this does nothing for my archive pages. I have no clue how to fix them...
    – josh
    Commented Jun 21, 2011 at 23:10
  • 1
    Instead of putting functions in functions, try using a class instead. Commented Jun 28, 2011 at 6:37

3 Answers 3


Use the query_vars or request filter to add orderby if orderby is not presented in the query string

  • +1 for request filter suggestion
    – Michal Mau
    Commented Dec 6, 2011 at 10:24
  • @josh also check out this answer wordpress.stackexchange.com/a/21378/2110 to see how to test for is_archive() and alike so early in the process.
    – Michal Mau
    Commented Dec 6, 2011 at 10:31
  • request filter was indeed, a good shout.
    – josh
    Commented Apr 14, 2014 at 14:42

Why don't you use query_posts()?

if ( have_posts() ) : while ( have_posts() ) : the_post();

endwhile; else:



You can also alter the query using pre_get_posts hook as well and do your conditional sorting there with is_archive(), is_page() etc...


I'm looking at your other question too.

If you wish to sort by the date meta-value they must naturally be in the same format - I'm guessing it's PHP's date('m-y') format as you specified in your code there?

If that's the case, you may find that if you change your meta_value in that code to meta_value_num you get a better result?

Not sure this will work, but might be worth a try.

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.