Changing order of posts
lilgee34 opened this issue · 8 comments
I have tried everything and I know this shouldn't be so hard. I created a catagory.php page and would like to change the order from post date to alphabetical.
Hi @lilgee34
You can use the filter pre_get_posts
to alter the order of the query run on the category (archive) page. You'll want to add the following little code snippet into a custom MU plugin that you create.
If you need help creating an MU plugin, we have a tutorial in the theme documentation, which you can find here: https://godaddy.github.io/wp-primer-theme/tutorials-and-examples/tutorials/mu-plugin.html
/**
* Order the archive page template by post title, ascending
*
* @param object $query Query object.
*/
function reorder_archive_template( $query ) {
if ( ! is_archive() || ! $query->is_main_query() ) {
return;
}
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );
}
add_action( 'pre_get_posts', 'reorder_archive_template' );
Once added to the MU plugin you can save the file and view your archive page, which should be sorted in ascending alphabetical order.
Let us know if that helps!
Evan
You need to include a <?php
tag at the top, as outlined in that documentation I've linked to.
Great! I'm glad we could help out and get that resolved. Have a great rest of your week!