Subscribe On YouTube

Join other subscribers and enjoy other Divi video tutorials!

How To Change The Order Of Projects In The Divi Portfolio Module

Nelson Miller Profile Orange
Our tutorial today will show you how to change the order of projects in the Divi Portfolio module alphabetically or randomly.

▶️ Please watch the video above to get all the exciting details! 👆

Add A PHP Code Snippet

The tutorial only requires one simple step, which involves pasting the PHP code snippet below into your website. There are a number of locations to add this, and it will totally depend on your website and experience.

Where To Paste The PHP Code

1. Divi Assistant
If you are using our Divi Assistant plugin, simply paste the code in the PHP tab in the custom code window in the Divi Visual Builder.

2. Child Theme
If you are using a child theme, paste this code into the functions.php file. If you don't have a child theme, you can generate a child theme directly on your site or download our free child theme.

3. Code Snippet Plugins
Otherwise, install a dedicated code snippet plugin, create a new snippet, and paste this code into the PHP code editor.

If you need help understanding where to paste the code, please check out our complete guide about where to add custom PHP code snippets in Divi.

PHP Snippet - Order Alphabetically By Title

add_filter('et_pb_module_shortcode_attributes', 'add_pre_get_portfolio_projects', 10, 3);
add_filter('et_pb_portfolio_shortcode_output', 'remove_pre_get_portfolio_projects');

function add_pre_get_portfolio_projects($props, $atts, $slug) {
$portfolio_module_slugs = array('et_pb_portfolio', 'et_pb_filterable_portfolio', 'et_pb_fullwidth_portfolio');
	if (!in_array($slug, $portfolio_module_slugs)) {
		return $props;
	}
	add_action('pre_get_posts', 'do_portfolio_pre_get_posts');

	return $props;
}

function do_portfolio_pre_get_posts($query) {
		
	do_action('pre_get_portfolio_projects', $query);
}
function remove_pre_get_portfolio_projects($content) {
	
	remove_action('pre_get_posts', 'do_portfolio_pre_get_posts');
	return $content;
}

//order projects by title

add_action('pre_get_portfolio_projects', 'pa_change_portfolio_module_order');
function pa_change_portfolio_module_order($query) {	
	$query->set('orderby', 'title');
$query->set('order', 'ASC');
}

Reverse The Order Z-A

If for some reason you want to show the items in the opposite order, you can edit the “ASC” in the code snippet. Simply replace the ASC with DESC the the items will show from Z to A.

Change Order To Random

You can also show the projects in Portfolio module in random order. For that, we will use the main part of the first snippet and only change the last part following the comment //order projects by title. So to make the order random, paste the above snippet, then remove the part after the // comment. In its place, paste the snippet below. Please watch the video above to see this in action, as it is important to get it correct.

Replacement PHP Snippet - Order Randomly

//order projects randomly

add_action('pre_get_portfolio_projects','pa_random_portfolio_module_order');

function pa_random_portfolio_module_order($query) {	

	$query->set('orderby', 'rand'); 
 }
Categories: Divi PHP Tutorials

Subscribe For More Things Like This!

At the start of each month, we send out a recap newsletter from the month before with family news, Divi news, our latest tutorials, and product news. Occasionally, if the news is too exciting to wait, we will send out another email separate from the monthly newsletter. That’s what you get when you subscribe, and of course you can unsubscribe if you are no longer interested!

Blog Post Optin

Leave A Response!

By commenting you agree to our Blog & YouTube Comments Policy

23 Comments

Comments By Others

  1. paul Milner

    Hi Thanks for the code;
    Does this work with Divi 5?

    Reply
      • Hemant Gaba

        Hi Paul!

        The code should work fine in Divi 5. However, can you share more details about the concern?

      • Paul Milner

        Thanks, Nelson

        I saw: add_filter(‘et_pb_module_shortcode_attributes’, in the code which refers to shortcodes which I think Divi 5 is all about removing.

  2. Elma

    Hi Nelson,
    Thanks for all your help! Unfortunately, this is not working for me. I am pasting your php code in a child theme directly under this code for the no-image crop from this tutorial. https://www.peeayecreative.com/how-to-stop-divi-image-crop/ Do I need to create a new child theme to host this code? Does it matter that it’s a filterable portfolio? Otherwise, I’ll try code snippets. Thanks!

    Reply
    • Nelson Lee Miller (aka The Divi Teacher) <span class="comment-author-role-label author-label">Author</span>

      Hi Elma,
      It’s difficult to know why it is not working. You can feel free to use a snippets plugin. You can only have one child theme active at a time, so it’s not about that.

      Reply
  3. Melissa Doyle

    Nelson – You are the most helpful guy!!

    Reply
  4. Phil

    is there a way to make sure numbered lists display from 1 to 9 not 1, 10, 11 etc..?

    Thanks heaps

    Phil

    Reply
  5. Simone

    Hi, I am so very grateful for all your advice – I watch your videos all the time!

    I did all the above (with Code Snippets plug-in); however, it doesn’t appear to be working for me. Is it possible, that the code must be worded in German in our case (based in Germany) or what could be the issue?

    Thanks a million!

    Reply
    • Hemant Gaba

      Hi Simone!

      The language should not cause any issue. Remote access to the website is needed to check whether there is some conflict happening with the PHP code. Please contact the Elegant themes support and ask them to check the function.php file remotely.

      Reply
  6. Caroline

    Hi, I used this – and it worked for the projects, but then we realized it was reordering the entire website and causing all sorts of problems. Has anyone else experienced this?

    Reply
    • Hemant Gaba

      Hi Caroline!

      Please note that the code will re order the projects and will reflect on all the portfolio modules. Please share some more details if you’re getting any other issue.

      Reply
  7. Caroline

    This is great! I really value all your trips. I’m looking to make use this, but I’m looking to make something Alpha order by Last name but the titles of the projects are First then Last name. So want is ordered Barney, but the title is Roger Barney.

    I could make a custom field with the last name (say the field name is “1lastn” )- but I’m trying to figure out how to change this:
    $query->set(‘orderby’, ‘title’);
    $query->set(‘order’, ‘ASC’);

    Could you help me?

    Reply
    • Hemant Gaba

      Hi Caroline!

      I’m afraid changing the order of the projects by last name is out of scope of this guide. However, we will look further into it.
      For now, please contact the Elegant themes support for an alternative.

      Reply
  8. Heike

    Hi Nelson,
    once again you just read my mind (or my to do list) 😉

    Is there also a way to order projects by category (even if there are different categories like year (numbers) and text)?
    I want to order it by year (which is party of my categories) in a descending order.

    Any ideas?

    Thanks a lot, I’m loving your plugins and your blog articles – your newsletter is the one I really read amongst all the others I just skip 🙂

    Reply
    • Hemant Gaba

      Hey Heike,

      We understood your query and will definitely look into the matter and see what’s best we can do.

      Reply

Submit a Comment

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

Recent Posts

0

Your Cart