Adding sorting to your Divi Blog Module based on a custom field created in Advanced Custom Fields (ACF) can significantly enhance the user experience on your site. In this post, we'll guide you step-by-step through sorting the posts in the Divi Blog Module by an ACF field, specifically sorting events by date.
An Example of the ACF-Based Sorting
Let's start by looking at a practical example. Below are screenshots illustrating the sorting feature:
- Before Sorting: Shows three event posts (Christmas, Halloween, and Black Friday) sorted by publish date (newest to oldest).
![](https://divibooster.com/wp-content/uploads/2024/09/image-8.png)
- After Sorting: Shows the same event posts sorted by an ACF date field (earliest to latest).
![](https://divibooster.com/wp-content/uploads/2024/09/image-9.png)
Set up the ACF Field
To begin, set up a date field in your ACF field group. We'll use a field with the key "event_date", like so:
![](https://divibooster.com/wp-content/uploads/2024/09/image-10.png)
Next, assign the new custom field a date in each of your posts, for example:
![](https://divibooster.com/wp-content/uploads/2024/09/image-11.png)
Add the PHP Code to your Site
Now add the following PHP code which initializes the sorting of the Divi Blog Module posts based on the ACF event_date
field:
add_filter('init', 'db_acf_sort_initialize_blog_module_sorting');
function db_acf_sort_initialize_blog_module_sorting() {
add_filter('et_pb_module_shortcode_attributes', 'db_acf_sort_add_sorting_filter', 10, 3);
}
function db_acf_sort_add_sorting_filter($attrs, $content, $module_slug) {
// Return if not a blog module
if ($module_slug !== 'et_pb_blog') {
return $attrs;
}
// Return if doesn't have "db-sort-by-acf-field" class set
if (empty($attrs['module_class']) || !in_array('db-sort-by-acf-field', explode(' ', $attrs['module_class']))) {
return $attrs;
}
add_filter('pre_get_posts', 'db_acf_sort_sort_blog_module_posts_by_acf', 10, 1);
add_filter('et_pb_blog_shortcode_output', 'db_acf_sort_remove_sorting_filter', 10, 2);
return $attrs;
}
function db_acf_sort_remove_sorting_filter($output, $attrs) {
remove_filter('pre_get_posts', 'db_acf_sort_sort_blog_module_posts_by_acf', 10);
return $output;
}
function db_acf_sort_sort_blog_module_posts_by_acf($query) {
$acf_field_name = 'event_date'; // Change this to your ACF field's name
$query->set('meta_key', $acf_field_name);
$query->set('orderby', 'meta_value');
$query->set('order', 'ASC'); // ASC for earliest events first, use DESC for reverse
return $query;
}
Related Post: Adding PHP to the Divi Theme
You can add the provided PHP code into the functions.php
file of your child theme to ensure the modifications are preserved even after theme updates. Alternatively, you can use a plugin like Code Snippets to manage and add custom code to your site without editing the theme files directly. This approach also makes it easier to enable, disable, or modify the snippets at any time.
To ensure it works on with your ACF field, change 'event_date' to your ACF field name at the point shown in the code, and if necessary, change the sort order from ASC to DESC to reverse the results.
Add a Class to the Module
To avoid affecting all blog modules on the site, the code above performs a check and only applies to a blog module if it has the "db-sort-by-acf-field" class. So, to enable the sorting on a particular blog module, add the "db-sort-by-acf-field" class at:
Blog Settings > Advanced > CSS ID & Classes > CSS Class
Like so:
![](https://divibooster.com/wp-content/uploads/2024/10/image-18.png)
Conclusion
By following these steps, you'll be able to sort your Divi Blog Module based on any ACF field, providing a more tailored and organized display of your content.
In my site I have different blog with different content (e.g. Event, Download, Article, etcetera) and only for Event I need to sort blog by event_date custom data
Using your code all blog are sorted by event_data but for Download and Article there no the custom data event_date so the blog say that no page are found because the sort function return void group.
How I can sort blog only for specific custom type content?
Hi Paolo,
Thanks for pointing this out. I've just updated the code in the post so that it only applies to blog modules which include the "db-sort-by-acf-field" class. I've also added details of how to add the class. If you replace the code on your site with the new version and add the class only to the blog module that displays your Events, I think that should resolve the issue. I hope that helps, but please let me know if you have any questions about it. Thanks!