Find out in this tutorial how to get WordPress to display the posts of a Custom Post Type sorted by date in the admin dashboard using code.
By default, WordPress sorts the entries of a Custom Post Type alphabetically in the admin. This can be handy if you don’t have many, but when there is a considerable amount and you are only interested in modifying the latest ones, it can be a bit cumbersome.
Luckily, with a bit of code you can get them sorted in the same way as the posts do, from newest to oldest.
If you have a project where you often create and modify CPT entries I’m sure you will find this tutorial very useful.
Snippet to display CPT entries sorted by date in WordPress admin
Add the following code snippet to your functionality plugin:
To see this and another 919 code snippets of this website, login or subscribe here.
In this snippet you use the pre_get_posts
filter to indicate that Custom Post Type “newsletter” posts are displayed from newest to oldest on the post’s administration page.
Now when you go to edit the posts of a CPT from the WordPress backend, the last ones you have created will appear at the top
To customize it to your needs just change the name of the CTP (newsletter) to the one you are interested in.
Conclusions
Do you want to access more quickly to the latest posts of a CTP in the WordPress administration page? Now you know how to do it by adding a simple snippet.
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. 🙂