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);
}
}
Related Post: Adding PHP to the Divi Theme
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.
Nothing works
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!