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.

Enhance Your Divi Site with Divi Booster!

Maximize the potential of your Divi site with Divi Booster. Easily customize and enhance your site's features, including sorting your project portfolio by ID, to create a seamless and powerful user 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.

Latest Posts

Hide the Page Title in the Hello Elementor Theme

Hiding the page title in the Hello Elementor theme allows you to create a cleaner and more customized page layout, free from default headings that may not fit your design. This is particularly useful for landing pages or custom content layouts where the default page...

How to Hide a Divi Module When Scrolling Up or Down

In this quick tutorial, I’ll show you how to hide a Divi module when scrolling in a specific direction (up or down), so that the module only shows when you want it to. This is especially useful when using Divi’s Scroll Effects feature and you have a effect you want to...

Fade a Divi Image Module Edge into the Background

Want to create a stylish fade effect on your Divi image module—where one side fades smoothly into the background? With a bit of CSS, you can make any edge (or corner) of the image fade out: top, bottom, left, right, or even diagonally.Fade a Divi Image Module Edge...

Hide the Header and Footer in the Hello Elementor Theme

Removing the default header and footer from your Hello Elementor theme allows for a streamlined and distraction-free website design. This is especially useful when creating unique landing pages, full-width layouts, or custom headers and footers with a page builder. In...

Setting up the Divi Password Box Module

Setting up password protection on a page can help you control access to sensitive or private content in WordPress, allowing only authorized visitors to view certain sections. With the Divi Password Box module, you can replace the plain Divi password form with a fully...

Random Posts