Puddinq.com sharing knowledge

Reorder posts in blog loop

Reorder posts in blog loop

If you want to sort by title

The code on the right is a individual loop you could place in wordpress. In the arguments the orderby is set to title, so posts will show alphabetically. Up and down can be set with DESC or ASC.

Reorder the main loop

If you want your frontpage blogroll (main) loop to be ordered alphabetically, you can use the following code changing the loop with pre get posts.

function order_by_title_mine( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'orderby', 'title' );
        $query->set( 'order', 'ASC' );
    }
}
add_action( 'pre_get_posts', 'order_by_title_mine' );
<?php $args = array(
	'posts_per_page'   => 5,
	'offset'           => 0,
	'category'         => '',
	'category_name'    => '',
	'orderby'          => 'title',
	'order'            => 'ASC',
	'include'          => '',
	'exclude'          => '',
	'meta_key'         => '',
	'meta_value'       => '',
	'post_type'        => 'post',
	'post_mime_type'   => '',
	'post_parent'      => '',
	'author'	   => '',
	'post_status'      => 'publish',
	'suppress_filters' => true 
);
$posts_array = get_posts( $args ); ?>

Then show the reordered loop thru as following:

<ul>
<?php

foreach ( $posts_array as $post ) : setup_postdata( $post ); ?>
	<li>
		<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
	</li>
<?php endforeach; 
wp_reset_postdata();?>

</ul>

The orderby can hold multiple options like the author, etc. If you want another option (like custom fields, posttypes) you can run multiple loops or order them just before rendering.