Change The Order Of Any Post Type In The Divi Blog Module
By default, the Divi Blog modules shows blog posts in chronological order based on the date the post was published, with the most recent post first. But sometimes there is a need to use the Divi Blog module to display posts, pages, projects, products, events, or any other custom post type. Most of those are not dependent on the publish date like blog posts, and because of that there may be a need to change the order to something else. In this tutorial, I am going to provide a code snippet which will sort posts, pages, or custom posts types in alphabetical order in the Divi Blog module. I hope you enjoy!
Be sure watch the video to see this in action! As a reminder, we have hundreds of other Divi tutorials, including an entire Divi Blog module series. You’ll find everything you need and didn’t know you needed there, and if you have any suggestions for new blog related tutorials let me know!
Join subscribers on our YouTube channel and enjoy other Divi video tutorials!
To change the default order of the Divi Blog module items, we need to override the existing code. We can do this by adding some PHP code to your Divi website.
Where To Place The Code
If you are using our free Divi child theme or any other child theme, place this snippet into the functions.php file. Otherwise, there may be some PHP code snippet plugins that you can use that do a similar function.
//change the order of posts/pages/cpt in the Divi Blog module
add_action('pre_get_posts', 'pa_change_blog_module_order');
function pa_change_blog_module_order($query) {
$pac = get_query_var( 'post_type' );
if ( 'post' == $pac )
{
$query->set('orderby', 'title');
$query->set('order', 'ASC');
}
}
Once you paste the code into your functions.php file, make sure to save the file and then go look at the results. Your Divi Blog module will show the items in alphabetical order from A to Z.
Other Post Types
The Divi Blog module has the option to show any post type. This means you can display a grid of pages, posts, projects, products, events, or any other custom post type that you have on your site.
In order to use anything other than the default blog posts, you will need to make a small edit to the snippet. All you have to do is change the “post” to match the post type that you are using. For example, for pages it would simply be “page” or for projects it would simply be “projects.”
Reverse The Order
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.
Is there any way to sort them in a random order?
Hi Becca,
Yes, just replace the orderby “title” with “rand” and it will do that.
Hi Nelson, I should order the articles of a specific blog module in a custom way. Let me explain: I have various articles shown on a page with a Divi blog, this is ok. Now I would need to have a new blog on another page of the website that shows the articles in a very specific order, not by date or alphabetical order but by an arbitrary and manageable choice (for example I would like a specific article on top, another article for second etc.). Is it possible to do this in some magical way? 😀
I like that idea, but I do not know how it would be done. It sounds more like you should not use a Blog module, just use other modules?
Thank you for the reply! you are probably right, I used the blog module to make my life easier on a website with many employees. On the Professionals page they are divided into 5 categories (partners, associates, etc) for each employee I created an article because it seemed the most sensible choice, especially graphically, because I would have used the blog module. But each employee (article) has specific areas of activity. The problem arises precisely in these ‘areas of activity’ pages where only the employed employees (articles) are listed and the customer would like a very specific order based on the category (therefore first the partners, then the others). sorting by date cannot work in this case, nor does alphabetical ordering .. maybe I could split multiple blog modules and use offset and number of articles shown ..
PS: the choice to use the blog derives from the fact that employees often change, leaving or adding new ones, changing categories, etc. the Blog module seemed perfect for this purpose!
Hi Nelson, thanks for this code. It works perfectly on my blog page. Anyway I have some other pages with different categories of posts and the posts are not in alphabetic order here. They have been built in theme builder. Any idea how to get them sorted in alph order as well? Thanks a lot.
Hi Lucie
We are looking into this issue and we will be updating the changes as soon as we fix them.
Thank you very much and awesome article. Do we know anything else about this? What Lucie says
Has this been fixed yet?
What about the Sticky option any way we can get this in the sort order.
EG I have 3 Sticky ones and only want to show 3 in my module but exactly these 3 ones.
I’m not sure what you mean, but I guess it’s not possible. Or at least, not without further development.
What would this look like for multiple custom post types? For example posts and projects
For this, you can just use the ||(this is the OR operator) and write the condition for the other post type. For example, if you want to write the code for both posts and the projects then the code will be:
function pa_change_blog_module_order($query) {
$pac = get_query_var( 'post_type' );
if ('project'== $pac || 'post'== $pac)
{
$query->set('orderby', 'title');
$query->set('order', 'ASC');
}
}
You can write as many conditions as you want for different post types. Please let us know if that helps. 🙂
Hi Nelson, I have gone through the process of creating a child theme and installing your PHP as seen in the video tutorial, but I can’t seem to get it to display my posts in descending alphabetical order.
Hi Kelly,
Not sure what to say, as the tutorial is working fine for me. Try double-checking everything.
Hi Nelson,
This is awesome! Any chance it can be adapted to the sortable portfolio module? I need to be able to sort the projects and have them displayed in alphabetical order…
Divi Blog Module allows us to choose from different post types present in WordPress so to display the Proejcts you can go to Blog Module Settings > Content Tab and there you can change the post type to projects. After that in the PHP code given above, you need to replace post word with projects present in if condition. Please let us know if that helps.
Great tutorial! Is there a code name for sorting out post in order of popularity? Thanks a lot
Popularity is a variable and can depend upon different parameters for different people but one parameter is common for every popular post and that is the number of views. It is obvious that if the post is popular then it will have more views and we can use this only parameter to sort the posts. If you search for the Popular Post Plugins then you will see various plugins which can help you with this in an advanced and easy manner.
But if you are keen to do it using PHP then there is a basic loop example for the Popular Post Sorting as per views:
4, ‘meta_key’ => ‘wpb_post_views_count’, ‘orderby’ => ‘meta_value_num’, ‘order’ => ‘DESC’ ) );
while ( $popularpost->have_posts() ) : $popularpost->the_post();
the_title();
endwhile;
?>
But my recommendation is to go for a plugin as it will give you more options.
Let us know if that helps. 🙂
Hi, Nelson,
I notice the code works only when we select the post type. In my case I have a custom content type with a custom category that I made a template with the Divi Theme Generator for that specific category template. I have used de blog module set up for post in the custom page. But it order by date.
Is there an specific code for what I want to do?
I’m not sure what you mean by “only when we select the post type.” I’m also not sure what a “custom content type” is and what the custom category is referring to. I guess there is a language translation issue.
Hi Nelson,
Great Tutorial!
I have a site with different blog divi modules, every blog module show a different post category, Is possible order only one of these blog category?
Thanks a lot!
Hi there,
I am afraid that PHP codes can be used for specific pages but it is not possible to write a PHP code for a specific module.
Let us know if you need any further assistance. 🙂
Hi Nelson,
I really appreciate you sharing this snippet. I think I have a similar query to a few others in that I’d like to control the order of the blog posts differently on the homepage to elsewhere.
if ( ‘post’ == $pac && $query->is_front_page() )
I’ve tried modifying the if statement like above but it doesn’t seem to work. The Front Page is set to a static page (so is_home()) can’t be used.
Any suggestions?
Hi Damo,
Could you please try using this if condition and let me know if that helps?
if ( 'page' == get_option('show_on_front') ) {
// do something
}
Would there be a way to do this only for a specific category? Thank you so much.
Hi Melissa,
If you want to sort the blogs using categories then you can simply use different blog modules with different categories selected in them and then you can easily position them according to your preference.
Let me know if that helps. 🙂
Would really love an option in the Divi blog module to be able to do this with one click.
I have some pages I like being the way they are and others I would like in Alphabetical order.
Please put this on the docket! 🙂
This comment is edited and the answer can be seen by going to the URL given below:
Answer: https://www.codepile.net/pile/ER1B9mog
Go to the URL, follow the instructions and let us know how it goes. 🙂
Hey!
I tried to use this but I get the error:
syntax error, unexpected ‘)’
I tried to play with the code, but I could not get it to work unfortunately
I would really love this feature but only on 1 page
Thanks in advance 🙂
Hi there!
A little code in the comment got cut and that’s the reason why the code gave that error. I have edited that comment so now can you please go to the URL given and use that code instead?
Let me know how it goes. 🙂
Hey Hemant,
I just tried the solution provided here: https://www.codepile.net/pile/ER1B9mog and getting syntax error, unexpected ‘<', expecting end of file. Can you please check again?
Ben,
You can see from the snippet in our post that it is unrelated, no such element is in our code, so it sounds like you have an unrelated issue in the functions.php file.
Hey, so, I have a list of blog posts that I want displayed in alphabetical order on one specific page of my site.
I have multiple blog modules on this page, each listing posts from a different category.
I only want the posts on this page to display alphabetically. Any and all other blog modules elsewhere on my site can stay chronological.
Nelson’s base code works great. I tried using Hemant’s code, but it doesn’t do anything, even when I use the ‘is_page()’ option.
I’m still on the learning curve with php, so I’m stumbling around trying to figure out how to make it work. I’m assuming it’s a fairly easy solution for someone with more knowledge than I.
Thanks for any help!
– J
So I have one specific page on my site that serves as an index for posts, and I would like to have the posts on that page listed in alphabetical order. However, they are not all under the same category.
All other posts listed elsewhere on the site can remain chronological.
I’m trying to use Hemant’s code, but I can’t seem to get it to work. Nelson’s original code works great to change everything sitewide, but I can’t seem to get it to work so that it only alphabetizes the posts on this one page.
I’m doing a bunch of trial and error on my end to see if I can’t figure it out, but any input in the meantime would be awesome! Thanks!
Hi Jason!
We’re able to replicate the issue with applying the code to specific page. We will check further and come up with a solution in our future guides. You can contact the Elegant themes support for such customization.
Thank you!
For some reason this isn’t working for me. I’m using a custom post type. I replaced the word post with it. But no change. The odd thing is that the posts are listed in DESC order by default. Like something is overriding this code. I want ASC.
Any ideas? Sorry, can’t share- private page.
Hi Sam,
Could you please share the name of the Custom Post Type and the exact code that you are using so that I could work with them to provide a solution?
Hi, thank you for your article. Your page helped me a lot.
I have a issue with sorting, I would like to sort the blog post regarding second word in title (used for list of artists).
Can I somehow achieve the sorting regarding second word?
Thank you
I’m glad to hear it helped! Sorry, I’m afraid I’m not sure how to filter by the second word and that is a beyond the scope of this tutorial.
Great article. I was wondering if there was a Divi trick for displaying blog posts by date range with a start and end date (ie older posts before 2020 or only posts from 2019).
Hi Jenn,
This sounds interesting. We will definitely try it out and see what can be done about it. 🙂
Would this work for the shop module? –
add_action(‘pre_get_posts’, ‘pa_change_shop_module_order’);
function pa_change_ehop_module_order($query) {
$pac = get_query_var( ‘post_type’ );
if ( ‘product’ == $pac )
{
$query->set(‘orderby’, ‘title’);
$query->set(‘order’, ‘ASC’);
}
}
Hi Reuben,
I don’t think the code you mentioned will work as Product is not a post type. We will look into this and provide the sorting code for the products asap.
It’s cool and works beautifully! I used the idea of a random appearance.
It works great on a blog but, in admin it displays the posts in a random way to:) Is there a way to fix this?
Hey Frenkel,
I guess it’s not possible as the code that we place in our child theme overwrites the code of the parent theme and displays the result on the frontend but we will definitely check if it can do the same in the backend as well.
Hi Nelson, so many comments already! I was just wondering if I can sort by ‘date’ and ‘ASC’ (yes, this works), but only on two different categories (A and B).
What do you think, I should add to your php?
Hey Rob,
We noted your query and will work on the snippet and share it in some time.
I’m new to Divi, and I was tearing out my hair because the PHP snippet I have used before with other themes just wasn’t working. Thank you, thank you, thank you!
You’re welcome!
Can this be set for specific post categories? Example, if you were to have a post category, or parent post category, of “recipes” and you want only that category ordered alphabetically but all other categories displayed normally?
Hi Nelson,
I modified your code a bit to check if it is in the homepage or frontpage. I do not want to change the blog module sorting in the front/homepage. This doesn’t seem to work, can you help me please?
if(! is_home()){
add_action(‘pre_get_posts’, ‘pa_change_blog_module_order’);
function pa_change_blog_module_order($query)
{
$pac = get_query_var( ‘post_type’ );
if ( ‘post’ == $pac )
{
$query->set(‘orderby’, ‘date’);
$query->set(‘order’, ‘ASC’);
}
}
}
Hey Jimmy,
I’ll check the code on my end and get back to you on this.
Great tutorial. I’ve played around with the functions.php but can’t seem to re-order Events descending. They currently sort by publish date and I want them to publish by actual event date but at least descending should give them to me in the order I need. Again, ideally, by event date so I only see the 3 upcoming events on the home page.
I’m afraid we can’t help with customization, sorry.
Very cool code snippet. Is there a way to have this type of sorting controllable on the Blog frontend by the user? Say they click a button that says “Sort by Title (ASC)” and it does?
Thanks!
Hey Sedona,
It is possible and we can use jQuery to achieve that functionality but this is out of scope for this guide.
Great article!
This works very nicely for Pages that use the Divi Blog Module but it doesn’t appear to work on Category Pages that use the Divi Blog Module.
I have some templates in the Divi Theme Builder that are applied to Specific Category Pages. In those Templates I’m using the Divi Blog Module. Is there anything I can do to have this work on Category Pages?
Thank you!
Hello, and thank you for this helpful code. I’m wondering why it only seems to work when the module is placed on an individual page, but not in the theme builder. I really need this feature on archive pages. Is there anything I can do to make this work?
Hey Marsha,
We haven’t tried this on our end but will do and get back on this.
Hey, cool code snippet – works fine – is there an option to show it only for dedicated divi blog? e.g. add a CSS ID “random_posts” to divi blog module – and only run code for that CSS ID? I tried https://www.codepile.net/pile/ER1B9mog but getting syntax error.
Appreciate your support.
Cheers
Ben
Hey Ben,
As the php code is being used to achieve the results so we can make it work for specific pages but not for specific blog module.
Hey,
thanks for sharing the code. Is there a way to run the code only for a dedicated blog CSS ID? I tried https://www.codepile.net/pile/ER1B9mog but getting syntax error.
Cheers
Ben
Ben,
You can see from the snippet in our post that it is unrelated, no such element is in our code, so it sounds like you have an unrelated issue in the functions.php file. You have extra ?> which is only meant for the very last line of a PHP file.
Hi Nelson,
functions.php issues resolved – thanks for the hint. However, the post-ID code is not working (still see divi blog module default sort order). The code above without post-id works straight away. Do you have any idea? Is there a way to do it via dedicated blog module css-id?
Cheers Ben
Sorry, we cannot provide any support beyond our actual code/tutorial.
Hi! this snippet was really useful. It shows in random order all the post…Is it possible to exclude a category?
I’m afraid not with this method, no.
What is wrong with my code? It sorts it by my custom field “event_date” but it wrecks the display on the frontend, where the divi elements or classes are apparently not loading.
add_action(‘pre_get_posts’, ‘pa_change_blog_module_order’);
function pa_change_blog_module_order($query) {
$pac = get_query_var( ‘post_type’ );
if ( ‘webinars’ == $pac )
{
$query->set( ‘orderby’,’meta_value’ );
$query->set( ‘meta_key’,’event_date’ );
$query->set( ‘order’,’ASC’ );
}
}
Hey Marc,
Will check the code on our end and get back to you on this.
Thank you very much for this. Works fine, except for one thing.
I’m from Sweden, and we use Å, Ä, and Ö in our alphabet. These letters are last after X, Y, and Z. But when I use this code, categories with either Å, Ä, or Ö are interpreted as A or O.
My web language is set to Swedish.
Do you know how to fix this? So that if I have categories with Å, Ä, or Ö they will come last in the list?
Best regards,
Fredrik
I don’t know, sorry.
Is it possible to sort by slug?
EDIT: I found the answer: just change ‘title’ by ‘name’:
//change the order of posts/pages/cpt in the Divi Blog module
add_action(‘pre_get_posts’, ‘pa_change_blog_module_order’);
function pa_change_blog_module_order($query) {
$pac = get_query_var( ‘post_type’ );
if ( ‘post’ == $pac )
{
$query->set(‘orderby’, ‘name’);
$query->set(‘order’, ‘ASC’);
}
}
Glad to hear you solved it!
The code worked great for my custom-post-type, but the results filter by column not by row. So, I am showing a list of employees and all the people with last names a – c are in the first column, the second column shows d-g, and the third column is showing h-l. Is there a way to list alphabetically by row, not by column?
I don’t understand what you mean, as the way this works for me is not as you described.
Thank you very much for your work!
Is there a way to order alphabetical in your “Custom Divi Blog Module” when I use “Included Taxonomy Terms”?
https://www.peeayecreative.com/how-to-show-custom-post-types-by-taxonomies-in-the-divi-blog-module-free-download/
I haven’t tried that, does the code work for it too? If not then it would need to be customized a bit.
I’m having some trouble getting this to work on just a specific page. What code is added, and where, to accomplish this?
The original snippet works perfectly, but I only want the posts to be sorted alphabetically on one specific page.
Thank you in advance.
Sorry since this is PHP it only works sitewide.
Hello, Nelson
How to configure the blog module to display the most viewed posts?
I think there may be plugins that track the most popular posts, but I have never used any myself and I am not sure how it would integrate with the Divi Blog module.
Hi Nelson, Thanks for the code. It works well. However, now all blogs in the website are arranged alphabetically. However, I want to arrange one blog by date and a second blog alphabetically. Is that possible?
I don’t know of any way to do this easily, it is possible but would require a different approach with excluding one.
Hello,
I have tried this snippet and it works perfectly , thank you!
Is it possible to use the ‘random’ order only on a specific blog module? I want to show two different blog modules per site, one static with products and one for ‘recommended products’ in random order.
Thanks for any answers in advance! 🙂
For PHP I’m not sure how to make it work for only one module, sorry!
Hi, as of today, it’s not working anymore :/ Used to work a few months ago. I don’t know why!
Hi Arnauld!
The issue could be happening due to any other plugin or custom code. Please disable all the plugins manually and comment out other PHP codes in function.php.
Let me know how it goes. 🙂
wondering if there is a way to use this code with ignoring article for example ‘a, a, the’ for the alphabetical sorting.
Hi Nate!
I’m afraid it is out of the scope of the guide. However, we will consider it for our future guides. Can you please share some more details of the feature?
The code as written originally is pretty awesome and works for me out of the box. So thank you for sharing that- but I’m hoping in the future that perhaps there will be a revised version where I can call out some sort of ‘If post & blog category’ situation. Right now i’m working on a tourism site so I’ve got to make a page for locations- but eventually we will have travel articles, location reviews ect and those will need to be sorted by date while the locations is fine being ASC order.
I tried to frankenstein two bits of PHP from a stack overflow conversation and this code from this tutorial- but I know absolutely nothing about PHP so obviously it didn’t work. Is there any that some variation could work?
//change the order of posts/pages/cpt in the Divi Blog module
add_action(‘pre_get_posts’, ‘pa_change_blog_module_order’);
function pa_change_blog_module_order($query) {
$pac = get_query_var( ‘post_type’ and get_the_ID()==206);
if ( ‘post’ == $pac )
{
$query->set(‘orderby’, ‘title’);
$query->set(‘order’, ‘ASC’);
}
}
Hi Tara!
I am afraid that PHP codes can be used for specific pages but it is not possible to write a PHP code for a specific module for now.
Let us know if you need any further assistance. 🙂
Thank you Hemant for responding so quickly! That could work- How would I call out for a specific page? I saw your code for the home page- but if it is on the locations page how would I specify that and where would it connect into the code? Like I said- I know nothing about PHP, so I don’t even know what to search to learn what to place where.
//change the order of posts/pages/cpt in the Divi Blog module
add_action(‘pre_get_posts’, ‘pa_change_blog_module_order’);
function pa_change_blog_module_order($query) {
$pac = get_query_var( ‘post_type’ and if ( ‘page’ == get_option(‘URL.com/locations’) );
if ( ‘post’ == $pac )
{
$query->set(‘orderby’, ‘title’);
$query->set(‘order’, ‘ASC’);
}
}
We’re able to replicate the issue with applying the code to specific page. We will check further and come up with a solution in our future guides. You can contact the Elegant themes support for such customization.
The provided functions code snippet works when I initially visit my DIVI blog page BUT, when I filter by category, They are not alphabetical.
Is there different code for category view?
Thank you,
Jim
This screen capture may shed more light. https://markuphero.com/share/P6sUPKcNdKXf5ZO17DjP
Thanks,
Jim
Hi Jim!
Thank you for sharing the issue. We’re aware of it and working for a solution. We’ll come up with one soon. Stay tuned!
Hello, I’m just checking back in after a few weeks to see if you were able to resolve this?
Thank you. I can’t get it to work in the theme builder, do you have an idea about that?
Hi Tonny!
I would suggest you to contact the Elegant themes support to check the issue remotely. The conflict could be with the theme builder.
Hi, I asked them, They will not help with this. They just asked me to contact Codeable or WP Buffs…
Hi, can you exclude a category? I have a movie blog and wont the categories to list post alphabetical. But I have a category call “Blog” I want to list the default way. So I need to exclude category Blog. Thanks
Hi Ronnie!
We cannot apply the code to a specific category only for now. We will look into the matter and may come up with a solution.
Thanks
How do you make it only for certain blog pages? If the coding you’ve shown is for all blog pages.
Hi Santosa,
It looks like you got it on the YouTube comment!