0

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 ?

1
  • Seems adding $posts = query_posts($query_string . '&orderby=title&order=asc'); is what i was looking for. Unless anyone knows of a better way ? Jan 23, 2013 at 16:05

1 Answer 1

4

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.

4
  • I have created a custom field directly in the posts table. How can i sort it with this field? It does not seem to work with your code
    – yeahman
    May 22, 2014 at 16:59
  • 1
    you added a column to the posts table? that's probably not a good idea. it won't work because WordPress has no knowledge of your column. use post meta instead and sort by that meta key.
    – Milo
    May 22, 2014 at 17:04
  • I was looking at the code; yup you are right, wp does not know this field and would require changing the core to do so.. so I will use meta data instead.. do you have an example how to add my meta field to the main wp_query and sort by this field?
    – yeahman
    May 22, 2014 at 17:14
  • ok got it I think: $query->set( 'meta_key', 'custom_order' ); $query->set( 'meta_type', 'NUMERIC' ); $query->set( 'orderby', 'customn_order' );
    – yeahman
    May 22, 2014 at 17:31

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.