Custom Sorting Portfolio Filter Bar
Code Snippet for Elementor Pro plug-in
Elementor is a beautiful and easy to use Page Builder for WordPress. It has a Pro version, which includes a Portfolio module with a Filter Bar.
Since version 1.5.5 of Elementor Pro (released July 3rd, 2017) the items in this filter bar are sorted by name (A-Z). Unfortunately, that’s the standard and there is no way to change it in the settings.
But what if you want a different sorting? E.g. by ‘count’ (the number of posts in a category or tag), the order of items in the back-end (menu order), or just the default ordering of WordPress, which is by date.
This ‘hack’ involves going into core files of Elementor Pro, so you have to make a note of it, as the changes will be overwritten every time the plug-in is updated.
The sorting of filter bar items is done in the file
\elementor-pro\modules\posts\widgets\portfolio.php
around lines 585-587. It looks like this:
usort( $terms, function( $a, $b ) {
return strcmp( $a->name, $b->name );
} );
To change this into sorting by menu order, make the following change:
usort( $terms, function( $a, $b ) {
return $a->term_order - $b->term_order;
} );
(Note: you do NOT use ‘menu_order’ here as you would in query options, but ‘term_order’ instead.)
Save the file and enjoy the new sorting of your portfolio filter bar!
But, but, you may ask: “How do I change the order of e.g. categories?”
Well, there’s a handy free plug-in for that: Simple Custom Post Order.
We think Elementor should add filter bar sorting as an option, just as with the order of the portfolio posts themselves, so you’d have a choice of e.g. order by ‘Date’, ‘Title’, ‘Count’, ‘Menu Order’ or ‘Random’.
If you want to know more about the ‘usort’ function in PHP, you can check the PHP Manual.
Other Tips & Tricks
Online Payments in Cambodia
Are online payments possible in Cambodia? Yes, they are. Read what we recommend for Cambodian eCommerce businesses.