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.

I am already using a small code to set default sort orderby a custom field called "Year" I need to add another orderby to also sort by "Post Title" in the same sort.

Sort = Year-Customfield (DESC) and Post-title (ASC)

Following is the code I'm currently using to sort by Year DESC.

add_filter('pre_get_posts', 'sort_arc');
function sort_arc($q) {
 if ($q->is_category) {
   $q->set('orderby', 'meta_value_num');
   $q->set('meta_key', 'the-year');
   $q->set('order', 'DESC');
 }
 return $q;
}

Thanx

------------------EDIT---------------------------------

The Code that stopped working, as explained below

add_action('pre_get_posts', 'check_meta_sort');
function check_meta_sort($query) {
if( $query->is_admin == 1 ) {
return;
    }
if( !$query->is_main_query() ) {
return;
}
if( !$query->is_archive == 1 ) {
return;
}
$custom_field = ( $_GET['sort'] ) ? stripslashes( $_GET['sort'] ) : '';
$custom_value = ( $_GET['sortorder'] ) ? stripslashes( $_GET['sortorder'] ) : '';
if( $custom_field ) {
$query->set( 'meta_key', $custom_field );
$query->set( 'orderby', $custom_field );
if( $custom_value ) {
$query->set( 'order', $custom_value );
}
}
}
share|improve this question
    
The second block is very different from the first. I think I get it but please explain in human terms what you want to do. (Looks like you are trying to alter the query based on URL parameters.) In what way does this not work? –  s_ha_dum Mar 28 '13 at 0:11
add comment

2 Answers

up vote 0 down vote accepted

You could try using the posts_orderby filter:

add_filter('pre_get_posts', 'sort_arc');
function sort_arc($q) {
 if ($q->is_category() && $q->is_main_query()) {
    $q->set('meta_key', 'the-year');
    add_filter('posts_orderby', 'custom_posts_orderby');
 }
 return $q;
}

where

function custom_posts_orderby($orderby) {
    global $wpdb;
    return $wpdb->postmeta.".meta_value+0 DESC, ".$wpdb->posts .".post_title ASC";
}

takes care of the custom ordering. We use meta_value+0 from the meta_value_num definition in /wp-includes/query.php.

Update:

Here is one idea how you could try to combine the two hooks:

add_filter('pre_get_posts', 'custom_pre_get_posts');
function custom_pre_get_posts($q) {
    if( $q->is_admin ) {
        return $q;
    }
    if( !$q->is_main_query() ) {
        //echo "DEBUG: !is_main_query() <pre>".print_r($q,true)."</pre>";
        return $q;
    }
    if( !$q->is_archive() ) {
        //echo "DEBUG: !is_archive() <pre>".print_r($q,true)."</pre>";
        return $q;
    }
    if ($q->is_category() && !isset($_GET['sort']) ) {
        $q->set('meta_key', 'the-year');
        add_filter('posts_orderby', 'custom_posts_orderby');
        //echo "DEBUG: is_category() <pre>".print_r($q,true)."</pre>";
        return $q;
    }
    $custom_field = ( $_GET['sort'] ) ? stripslashes( $_GET['sort'] ) : '';
    $custom_value = ( $_GET['sortorder'] ) ? stripslashes( $_GET['sortorder'] ) : '';
    if( $custom_field ) {
        $q->set( 'meta_key', $custom_field );
        $q->set( 'orderby', $custom_field );
        if( $custom_value ) {
            $q->set( 'order', $custom_value );
            //echo "DEBUG: custom_value <pre>".print_r($q,true)."</pre>";
            return $q;
        }
        //echo "DEBUG: custom_field <pre>".print_r($q,true)."</pre>";
        return $q;
    }
    //echo "DEBUG: else <pre>".print_r($q,true)."</pre>";
    return $q;
}
share|improve this answer
    
Thanx Birgire, That works well, but it has made my other function stop working, The other function allows user to sort by Year ASC and DESC. I have edited the main post to add new code. –  Naveen Narale Mar 22 '13 at 8:04
    
Revo, I'm sorry but that did not work. The sorting function still does not work. –  Naveen Narale Mar 22 '13 at 11:15
    
Maybe you could add extra conditions in the if-sentence, like !isset($_GET['sort']) && !isset($_GET['sortorder'])? –  birgire Mar 22 '13 at 11:20
    
Birgire, I'm not sure how I can do that, can you please elaborate? –  Naveen Narale Mar 22 '13 at 11:26
    
by the way do you have to pre_get_posts hooks? Can you combine them into one filter? –  birgire Mar 22 '13 at 11:37
show 11 more comments

According to @birgire answer and your comment:

change your second snippet add_action to add_filter then set priorities for their execution.

In your case this should work:

add_filter('pre_get_posts', 'check_meta_sort', 9); // earlier execution
add_filter('pre_get_posts', 'sort_arc', 10);
share|improve this answer
    
@NaveenNarale how about not to changing add_action but just the priorities? –  revo Mar 22 '13 at 11:15
    
add_action('pre_get_posts', 'check_meta_sort', 9); add_filter('pre_get_posts', 'sort_arc', 10); That did not work, let me know if I'm missing anything here. –  Naveen Narale Mar 22 '13 at 11:29
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.