Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Divi - Change the order for the default filterable portfolio module]
<?php
// Add this snippet to your themes "functions.php" to order the projects by title
add_action( 'parse_query', function( $vars )
{
if ( 'project' == $vars->query['post_type'] )
{
$vars->set( 'orderby', 'title' );
$vars->set( 'order', 'ASC' ); // DESC
} // end if
}); // end add_action
@jan1980

This comment has been minimized.

Copy link

@jan1980 jan1980 commented Aug 18, 2018

That was excellent thanks...!

@guidoeffe

This comment has been minimized.

Copy link

@guidoeffe guidoeffe commented Oct 15, 2018

Thanks a lot!
It's simple but magic...

@constmoon

This comment has been minimized.

Copy link

@constmoon constmoon commented Jan 2, 2019

This is really a life saver…

@metrosuperstar

This comment has been minimized.

Copy link

@metrosuperstar metrosuperstar commented Mar 11, 2019

To the author: thank you! This is very helpful. However, what would the code look like if we wanted to target 2 different portfolio modules according to their class name in Divi? (because the other would be sorted by last modified, instead of title).
I'm assuming that if(class_exists("")){ needs to be used - but my syntax didn't work.

@OvidiuZeicu

This comment has been minimized.

Copy link

@OvidiuZeicu OvidiuZeicu commented Jan 6, 2020

Thank you very much, it worked great for quite a while. Unfortunately, since Divi 4.0 (or around that version) it no longer works and crashes the website. Something along the lines: "Website is experiencing a PHP error", after I removed this code everything was working again, but I could not add it again in the theme editor, saving fails, sometimes with an "Unable to communicate back with site to check for fatal errors, so the PHP change was reverted. You will need to upload your PHP file change by some other means, such as by using SFTP." error, but lately with "Your PHP code changes were rolled back due to an error on line 578 of file wp-includes/functions.php. Please fix and try saving again. Allowed memory size of 268435456 bytes exhausted (tried to allocate 20480 bytes)" error.
That 578 line is part of a function for "Unserialize value only if it was serialized.".
I'm pretty sure all it needs it's a small adjustment in the function to work again, but PHP is not something I have skills in. Could you look into it, please?

@dgrimey

This comment has been minimized.

Copy link

@dgrimey dgrimey commented Feb 3, 2020

Thanks for posting. Saved me a ton of time with one quick Google search.

@brandylynn02

This comment has been minimized.

Copy link

@brandylynn02 brandylynn02 commented Mar 19, 2020

Can anyone edit this to filter by slug instead of title?

@benjaminlately

This comment has been minimized.

Copy link

@benjaminlately benjaminlately commented Jan 30, 2021

The code above works well but will also filter the back end which might be a pain...

This is a quick fix to only sort on the public facing side of the site.

// Order the portfolio items by title
add_action( 'parse_query', function( $vars ) {
if (class_exists("ET_Builder_Module") && 'project' == $vars->query['post_type'] )
{
$vars->set( 'orderby', 'title' );
$vars->set( 'order', 'ASC' );
}
}); // end custom order

@tecnologiasos

This comment has been minimized.

Copy link

@tecnologiasos tecnologiasos commented Oct 13, 2021

Thank you very much!!! This code is amazing, and save my life!!! I was thinking in remake an filterable portfolio with more than 600 entrees or change their dates one by one. The upgrade done by @benjaminlately works perfectly on my site. Thanks again from Caracas Venezuela.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment