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've got the following code which i found somewhere else i use it for pagination.

I don't understand all of it although I've read codex.wordpress.org/Class_Reference/WP_Query several times. they seem to speak about new queries.

global $wp_query, $wp_rewrite, $numposts;
$wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;  
$pagination = array(  
    'base' => @add_query_arg('page','%#%'),  
    'format' => '',  
    'total' => $wp_query->max_num_pages,  
    'current' => $current,  
    'show_all' => false,
    'mid_size' => 5, 
    'type' => 'plain',
    'prev_next'    => True,
    'prev_text'    => __('[<< Prev]'),
    'next_text'    => __('[Next >>]'),
);  
if ( $wp_rewrite->using_permalinks() ) 
    $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );  

if ( !empty($wp_query->query_vars['s']) ) 
    $pagination['add_args'] = array( 's' => get_query_var( 's' ) );

Is there a way to change this so it will also sort the posts by lets say title or name or id or any other type of sorting ?

share|improve this question
    
Seems adding $posts = query_posts($query_string . '&orderby=title&order=asc'); is what i was looking for. Unless anyone knows of a better way ? –  Yaniv Kossas Jan 23 '13 at 16:05
add comment

1 Answer

Use pre_get_posts to do any alterations of the main query. The main query happens before the template is loaded, using query_posts just overwrites that original query, which is a waste of resources:

function wpa82795_archives_orderby( $query ) {
    if ( $query->is_archive() && $query->is_main_query() ) {
        $query->set( 'orderby', 'title' );
        $query->set( 'order', 'asc' );
    }
}
add_action( 'pre_get_posts', 'wpa82795_archives_orderby' );

See Conditional Tags for other conditions you can test for to limit the application of your new query parameters. You can set any query arguments that are available for WP_Query.

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.