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

Set Custom CSS IDs for Individual Divi Accordion Items

Assigning unique CSS IDs to specific Divi Accordion items allows for precise control over styling, targeting, and linking within your page content. This ability is particularly useful when you want to apply custom designs or create anchor links to particular accordion...

Enable Swipe Navigation in the Divi Gallery Lightbox

Enabling swipe navigation in the Divi Gallery module's lightbox allows users to seamlessly browse through gallery images by swiping left or right, creating a more interactive and mobile-friendly experience. This functionality can significantly improve user engagement...

Disable Slide-In Animation for Divi Gallery Grid Images

Control how images appear in your Divi Gallery module by toggling the slide-in animation effect for grid layouts. Disabling the slide-in animation allows gallery images to load instantly and appear statically, providing a faster and distraction-free browsing...

Control Image Count Display in Divi Gallery Lightbox

Displaying or hiding the image count in the Divi Gallery module’s lightbox can help customize the user experience, depending on whether you want to give visitors an indication of gallery progress or prefer a cleaner, distraction-free view. The ability to toggle this...

Hide Gallery Image Titles in the Divi Lightbox Overlay

Displaying image titles in the lightbox overlay of the Divi Gallery module can sometimes be distracting or unnecessary, depending on your website’s design and user experience goals. Hiding these titles creates a cleaner and more focused viewing experience for visitors...

Random Posts