Dev Logging
I wanted to stat a Dev Log for a VR project I am working on but was always frustrated that individual posts in WordPress are presented in reverse chronological order, making is so a “story” appears to run backwards. I asked ChatGPT for some ideas and it suggested:
- Using a Plugin: There are plugins like “Post Types Order” and “WP Query Order” that allow you to easily change the order of posts in categories through the WordPress admin interface.
- Modify the Theme’s
functions.php
File: You can also change the order by adding code to your theme’sfunctions.php
file.- Create a Category Template: If you want to change the order only for specific categories, you can create a category template. Create a file called
category-your_category_slug.php
in your theme’s directory and use a custom query to display posts in chronological order.
Initially, I thought the idea of a plugin was easier than trying to hack the functions.php file, but each of the plugins I looked at (briefly) seemed to offer “drag & drop” resorting, but I just wanted to change the sort order by date. (I may regret this as I now realize if I update a post, I need to remember to maintain the old date to keep the order)
Just for the record, the one plugin I tried was “Post Types Order” and to be fair, I played with it for all of 5 minutes.
While trying to find a code syntax highlighter, I stumbled across “Code Snippets”, a plugin that allows you to effectively update the functions.php from the admin panel. With this, I added the code snippet that ChatGPT provided with option 2:
function chronologically_ordered_category( $query ) {
if ( $query->is_category() && $query->is_main_query() ) {
$query->set( 'orderby', 'date' );
$query->set( 'order', 'ASC' );
}
}
add_action( 'pre_get_posts', 'chronologically_ordered_category' );
With this, when my Categories are viewed, they are in the desired chronological order.