Learn in this tutorial how to change the order of the entries of a Custom Post Type in the archive page so that the oldest ones is displayed first.
By default, WordPress displays the newest entries at the top of the archive page.
This can be ideal for a blog, but for some CPTs, such as courses or lessons, you may want to display them in chronological order.
Here’s how to do it using the pre_get_posts hook and WordPress conditional tags.
Snippet to change the order of entries in a CPT’s file page
Add the following code at the end of functions.php or in your functionality plugin:
In this fragment you use the hook ‘pre_get_posts’ to sort the CPT entries by date in ascending order.
You also use the conditional tag is_archive to apply this logic to the archive page.
The code is intended for the CPT ‘course’, but you can replace it with any CPT you want.
Thanks to this tutorial you now know how to modify the order of the entries of a CPT in the archive page of your website with WordPress.
If you have any question, please leave it in the comments. And if you want to give me a suggestion for future snippets, please send it through the contact form.
Benefits of being a subscriber. 🙂