Skip to content

Instantly share code, notes, and snippets.

@morgyface
Last active January 7, 2017 16:53
Show Gist options
  • Select an option

  • Save morgyface/956126549c4a88e8b27fa65fd9dd9125 to your computer and use it in GitHub Desktop.

Select an option

Save morgyface/956126549c4a88e8b27fa65fd9dd9125 to your computer and use it in GitHub Desktop.
WordPress | Loop | Order | pre_get_posts
<?php
// This alters the main loop (loop.php) to order Archive queries by Title
function archive_posts($query) {
if ( $query->is_archive() ) {
$query->set( 'orderby', 'title' );
$query->set( 'order', 'asc' );
}
}
add_action('pre_get_posts', 'archive_posts');
?>
@morgyface
Copy link
Author

morgyface commented Jan 7, 2017

Loop change

The main loop.php template just dishes out the posts in date order by default. Here we use pre_get_posts to change the order of the query when it is an archive.

This needs adding to functions.php.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment