How To Change The Order Of Projects In The Divi Portfolio Module Tutorial By Pee Aye Creative

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

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

Join subscribers on our YouTube channel and enjoy other Divi video tutorials!

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.

If you have a child theme, then you would add this snippet to the functions.php file of a Divi child theme. If you don’t already have one, you can download our free Divi child theme here.

If you want the easy way, then I will recommend installing the Code Snippets plugin. It is a great plugin that allows you to add snippets easily in the backend of your admin area. After activating, just go to Snippets>Add New, write a title, paste the snippet, and click the save and activate button. Here is how that will look:

PHP snippet to change the order of the Divi Projects in the Portfolio module

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'); 
 }

Last updated [last-modified %date%]

Subscribe

Each month we send out a roundup email newsletter with the latest tutorials, product updates, helpful resources, and any other industry or personal news. Occasionally we send an extra separate email here and there if we just can’t wait! So that’s what you will get if you subscribe, and you can always unsubscribe at any time if you just can’t take it anymore :)

Blog Post Optin

Please share this post!

Nelson Lee Miller (aka The Divi Teacher)

Nelson is the owner of Pee-Aye Creative in the beautiful state of Pennsylvania. He loves helping small businesses, exploring outdoors, building websites with Divi, and teaching others.

Leave A Response!

By commenting you agree to our Blog & YouTube Comments Policy

16 Comments

  1. 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
  2. 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
  3. 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
  4. 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
  5. Phil

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

    Thanks heaps

    Phil

    Reply
      • Phil

        Yup, I implemented the code, each portfolio post has a number before the Text.. I need the numbers to be sequential. currently it is showing 1 then 10 then 11 then 12 etc. see link for page I have implemented it on. https://pataka.ac.nz/52-weeks/

      • Phil

        I was able to address this issue if you are interested. Thanks for getting me on the right track and getting back to me. I tried copying all the code in here but the system didn’t allow it.

        Cheers

        Phil

Submit a Comment

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

Recent Posts

0

Your Cart