Alphabetical Or Random Sorting Options
By default, the Divi Portfolio module shows projects in chronological order based on the date the project was published, with the most recent project first. But this is not useful in most cases, and it would be better to have other sorting options. So in this tutorial, I am going to show you how to change the order of projects in the Divi Portfolio and Filter Portfolio modules by using a PHP code snippet. With this guide you will be able to sort the items in alphabetical order, reverse-alphabetical order, and random order.
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 - 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');
}
Thanks for this!
You’re welcome, glad you like it!
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 🙂
Hey Heike,
We understood your query and will definitely look into the matter and see what’s best we can do.
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?
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.
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?
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.
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!
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.
is there a way to make sure numbered lists display from 1 to 9 not 1, 10, 11 etc..?
Thanks heaps
Phil
Hi Phil,
I don’t know what you mean. Is this related to our tutorial somehow?
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/
I really don’t know, sorry.
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
If it is something to improve the code in the tutorial, sure I would be happy to hear about that. You can send us a message with the code: https://peeayecreative.com/contact/