How to Sort Divi Blog Module Posts by an ACF Field

Written by Dan Mossop

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).
  • After Sorting: Shows the same event posts sorted by an ACF date field (earliest to latest).

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:

Next, assign the new custom field a date in each of your posts, for example:

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;
}

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:

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. 

Do More with Divi Dynamic Content!

Enhance your site's flexibility with the Divi Dynamic Content Extended plugin. Unlock dynamic content on more module fields and access data from ACF, Meta Box, and Pods settings pages, making your Divi designs smarter and more efficient.

About Dan Mossop

Dan is a Scottish-born web developer, now living in Brisbane with his wife and son. He has been sharing tips and helping users with Divi since 2014. He created Divi Booster, the first Divi plugin, and continues to develop it along with 20+ other Divi plugins. Dan has a PhD in Computer Science, a background in web security and likes a lot of stuff, 

2 Comments

  1. 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?

    Reply
    • 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!

      Reply

Submit a Comment

Comments are manually moderated and approved at the time they are answered. A preview is shown while pending but may disappear if your are cookies cleared - don't worry though, the comment is still in the queue.

Your email address will not be published. Required fields are marked *.

We may earn a commission when you visit links on our website.