Customizing the sorting order of your blog posts can enhance user experience and help highlight your most important content.
In this article, we will guide you through the process of changing the default sorting order of posts in Divi's Blog Module.
There are two methods we can use:
Method 1: Use a PHP snippet (without a child theme) - recommended
Method 2: Use a Child Theme
Method 1: Use a PHP snippet (without a child theme) - recommended
Change the Post ordering for all Blog modules
Go to WordPress Dashboard → Plugins and install the Code Snippets plugin.
Create a new PHP Snippet.
Give the snippet a title and paste in the following PHP code:
function custom_blog_order($query, $args) {
$query->query_vars['orderby'] = 'title';
$query->query_vars['order'] = 'desc';
$query = new WP_Query($query->query_vars);
return $query;
}
add_filter('et_builder_blog_query', 'custom_blog_order', 10, 2);Save and activate the snippet.
Note: The above PHP snippet will change the post order based on their titles, with the order descending. This will apply to all Blog modules on your website.
Change the Post ordering only on specific Blog modules
If you have multiple instances of the Blog module on your website, but you want to change the default post order only for specific Blog modules, follow the steps below:
Go to WordPress Dashboard → Plugins and install the Code Snippets plugin.
Create a new PHP Snippet.
Give the snippet a title and paste in the following PHP code:
function custom_blog_order_class($query, $args) {
if (isset($args['module_id']) && $args['module_id'] === 'dt-post-order-by-title') {
$query->query_vars['orderby'] = 'title';
$query->query_vars['order'] = 'ASC';
$query = new WP_Query($query->query_vars);
}
return $query;
}
add_filter('et_builder_blog_query', 'custom_blog_order_class', 10, 2);Edit the Blog module(s) for which you want to change the default post ordering
Go to the Advanced Tab → CSS ID & Classes → CSS Class
Type in
dt-post-order-by-title
Note: Only the Blog modules with the CSS class dt-post-order-by-title
will have their posts ordered by title in Ascending order.
In both cases, you can change the values for:
$query->query_vars['orderby']
and
$query->query_vars['order']
For more information on ordering, please visit the WordPress Developer Documentation page.
Method 2: Use a Child Theme
Note: Before continuing with this article, ensure the following:
A Child theme for Divi is installed and active
The Blog.php file has been copied from the Parent theme to the child theme.
Go to Appearance → Theme File Editor
Open the Blog.php file from your Child theme folder
Search this piece of PHP code:
$args['cat'] = implode( ',', self::filter_include_categories( $include_categories ) );
$args['paged'] = $et_paged;Add this PHP code right after it:
$args['orderby'] = 'title';
$args['order'] = 'asc';Save your changes.
The Posts will not be displayed in Ascending order based on their titles.