Change The Order Of Any Post Type In The Divi Blog Module
By default, the Divi Blog modules shows blog posts in chronological order based on the date the post was published, with the most recent post first. But sometimes there is a need to use the Divi Blog module to display posts, pages, projects, products, events, or any other custom post type. Most of those are not dependent on the publish date like blog posts, and because of that there may be a need to change the order to something else. In this tutorial, I am going to provide a code snippet which will sort posts, pages, or custom posts types in alphabetical order in the Divi Blog module. I hope you enjoy!
Be sure watch the video to see this in action! As a reminder, we have hundreds of other Divi tutorials, including an entire Divi Blog module series. You’ll find everything you need and didn’t know you needed there, and if you have any suggestions for new blog related tutorials let me know!
To change the default order of the Divi Blog module items, we need to override the existing code. We can do this by adding some PHP code to your Divi website.
Where To Place The Code
If you are using our free Divi child theme or any other child theme, place this snippet into the functions.php file. Otherwise, there may be some PHP code snippet plugins that you can use that do a similar function.
//change the order of posts/pages/cpt in the Divi Blog module
add_action('pre_get_posts', 'pa_change_blog_module_order');
function pa_change_blog_module_order($query) {
$pac = get_query_var( 'post_type' );
if ( 'post' == $pac )
{
$query->set('orderby', 'title');
$query->set('order', 'ASC');
}
}
Once you paste the code into your functions.php file, make sure to save the file and then go look at the results. Your Divi Blog module will show the items in alphabetical order from A to Z.
Other Post Types
The Divi Blog module has the option to show any post type. This means you can display a grid of pages, posts, projects, products, events, or any other custom post type that you have on your site.
In order to use anything other than the default blog posts, you will need to make a small edit to the snippet. All you have to do is change the “post” to match the post type that you are using. For example, for pages it would simply be “page” or for projects it would simply be “projects.”
Reverse The Order
If for some reason you want to show the items in the opposite order, you can edit the “ASC” in the code snippet. Simply replace the ASC with DESC the the items will show from Z to A.