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.

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.

share|improve this question
    
You should prefix all your function with for eg. josh_ to avoid naming colisions. –  kaiser Jun 21 '11 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 Jun 21 '11 at 9:31
1  
Yes it's good practice. Plus: You shouldn't wrap functions in functions. –  kaiser Jun 21 '11 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 Jun 21 '11 at 23:10
1  
Instead of putting functions in functions, try using a class instead. –  Manny Fleurmond Jun 28 '11 at 6:37
show 8 more comments

3 Answers

up vote 2 down vote accepted

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

share|improve this answer
    
+1 for request filter suggestion –  Michal Mau Dec 6 '11 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 Dec 6 '11 at 10:31
    
request filter was indeed, a good shout. –  josh Apr 14 at 14:42
add comment

Why don't you use query_posts()?

<?php
query_posts('meta_key=key_name&orderby=key_name&sort=ASC');
if ( have_posts() ) : while ( have_posts() ) : the_post();

endwhile; else:

endif;

wp_reset_query();

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...

share|improve this answer
add comment

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.

share|improve this answer
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.