Use Divi Booster's Sort Portfolio By ID Option with Custom IDs

Written by Dan Mossop

Divi Booster includes the option to sort projects in the Divi Portfolio module. One of the available sort options is "by ID" which lets you specify the exact order of projects in the portfolio by providing a list of the projects' post IDs. If you use your own custom project IDs, you may find it more convenient to use these custom IDs in the Sort by ID option. Here's how to enable this.

Use Custom Project IDs stored in a Custom Field

If you store your custom project IDs in a standard WordPress custom field in each project, like so:

Then you can use the following PHP code to automatically translate your custom project IDs into the regular post IDs required by the Divi Booster option:

$portfolio_custom_field_sorter = (new DBDB_sort_portfolio_by_custom_field_id('my-project-id'));

add_filter('dbdb_et_pb_module_shortcode_attributes', array($portfolio_custom_field_sorter, 'add_pre_get_posts_filter'), 9, 3);
add_filter('et_module_shortcode_output', array($portfolio_custom_field_sorter, 'remove_pre_get_posts_filter'));

class DBDB_sort_portfolio_by_custom_field_id {
	
	private $projects = array();
	private $custom_field_name;
	
	function __construct($custom_field_name) {
		$this->custom_field_name = $custom_field_name;
	}
	
	function add_pre_get_posts_filter($props, $atts, $slug) {
		
		if ($slug !== 'et_pb_portfolio') { return $props; }
		if (empty($atts['db_project_order']) || $atts['db_project_order'] !== 'by_id') { return $props; }
		if (empty($atts['db_project_order_ids'])) { return $props; }
		
		$ids = array_map('trim', explode(',', $atts['db_project_order_ids']));
		$this->projects = array();
		foreach ($ids as $id) {
			$posts = get_posts(array(
				'numberposts'   => 1,
				'post_type'     => 'project',
				'meta_key'      => $this->custom_field_name,
				'meta_value'    => $id
			));
			if (isset($posts[0]) && isset($posts[0]->ID)) {
				$this->projects[] = $posts[0]->ID;
			}
		}
		
		add_action('pre_get_posts', array($this, 'set_query_order'), 12);
		return $props;
	}
	
	function remove_pre_get_posts_filter($content) {
		remove_action('pre_get_posts', array($this, 'set_query_order'), 12);
		return $content;
	}

	function set_query_order($query) {	
		$query->set('post__in', $this->projects);
	}
}

To ensure it works with your custom field, simply replace 'my-project-id' (in the first line of the code) with the name of your custom field.

Now you can use your custom IDs in the Project IDs field, like so:

Note that this code is designed to work with the standard built-in WordPress custom fields, rather than custom fields managed by a third-party plugin such as Advanced Custom Fields.

Customize Your Portfolio with Divi Booster

Make your Divi projects stand out by using Divi Booster's advanced sorting options, including sorting by custom project IDs. Perfect for tailoring your portfolio exactly how you want it. Take advantage of hundreds of other features to simplify and enhance your Divi Builder experience.

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. Nothing works

    Reply
    • Hey Lulu.

      Thanks for pointing this out I've looked into the issue, and it turns out a change I made in a recent Divi Booster update (which affected action hook priorities) was preventing the code in this post from working correctly. The fix was changing the add_action / remove_action priority level in the code from 11 to 12, which is now updated in the code in the post.

      Please give the updated code a shot, but if it's still not working please let me know.

      Lastly, just a heads-up: the correct order you're after will only show up on the actual live site, not in the visual builder's preview.

      Cheers!

      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.