How To Sort Divi Blog Module Posts In Alphabetical Order Tutorial by Pee Aye Creative

How To Sort Divi Blog Module Posts In Alphabetical Order

Our tutorial today will show you how to sort blog posts or other custom posts types in the Divi Blog module in alphabetical order.

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.

Last updated Aug 31, 2021 @ 11:01 am

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 :)

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

80 Comments

  1. Becca

    Is there any way to sort them in a random order?

    Reply
  2. Federico

    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? 😀

    Reply
      • Fede

        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 ..

      • Fede

        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!

  3. Lucie

    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.

    Reply
    • Hemant Gaba

      Hi Lucie

      We are looking into this issue and we will be updating the changes as soon as we fix them.

      Reply
      • Ruben

        Thank you very much and awesome article. Do we know anything else about this? What Lucie says

      • Hannah

        Has this been fixed yet?

  4. Björn Bürke

    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.

    Reply
  5. Mike

    What would this look like for multiple custom post types? For example posts and projects

    Reply
    • Hemant Gaba

      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. 🙂

      Reply
  6. Kelly Skelton

    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.

    Reply
  7. Audrey

    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…

    Reply
    • Hemant Gaba

      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.

      Reply
  8. Angelique

    Great tutorial! Is there a code name for sorting out post in order of popularity? Thanks a lot

    Reply
    • Hemant Gaba

      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. 🙂

      Reply
  9. Xesús

    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?

    Reply
    • Nelson Lee Miller (aka The Divi Teacher)

      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.

      Reply
  10. Pep

    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!

    Reply
    • Hemant Gaba

      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. 🙂

      Reply
  11. Damo

    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?

    Reply
    • Hemant Gaba

      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
      }

      Reply
  12. Melissa

    Would there be a way to do this only for a specific category? Thank you so much.

    Reply
    • Hemant Gaba

      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. 🙂

      Reply
  13. Johnny Ringo

    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! 🙂

    Reply
    • Hemant Gaba

      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. 🙂

      Reply
      • DTG

        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 🙂

      • Hemant Gaba

        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. 🙂

      • Ben

        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?

      • Nelson Lee Miller (aka The Divi Teacher)

        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.

  14. Sam Lowry

    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.

    Reply
    • Hemant Gaba

      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?

      Reply
  15. Dejw

    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

    Reply
    • Nelson Lee Miller (aka The Divi Teacher)

      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.

      Reply
  16. Jenn

    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).

    Reply
    • Hemant Gaba

      Hi Jenn,

      This sounds interesting. We will definitely try it out and see what can be done about it. 🙂

      Reply
  17. Reuben

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

    Reply
    • Hemant Gaba

      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.

      Reply
  18. aya frenkel

    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?

    Reply
    • Hemant Gaba

      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.

      Reply
  19. Rob

    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?

    Reply
    • Hemant Gaba

      Hey Rob,

      We noted your query and will work on the snippet and share it in some time.

      Reply
  20. Lisa-Ann Oliver

    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!

    Reply
  21. Chris

    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?

    Reply
  22. Jimmy R

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

    Reply
    • Hemant Gaba

      Hey Jimmy,

      I’ll check the code on my end and get back to you on this.

      Reply
  23. Laura

    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.

    Reply
  24. Sedona

    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!

    Reply
    • Hemant Gaba

      Hey Sedona,

      It is possible and we can use jQuery to achieve that functionality but this is out of scope for this guide.

      Reply
  25. Brent

    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!

    Reply
  26. Marsha

    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?

    Reply
    • Hemant Gaba

      Hey Marsha,

      We haven’t tried this on our end but will do and get back on this.

      Reply
  27. Ben

    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

    Reply
    • Hemant Gaba

      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.

      Reply
    • Nelson Lee Miller (aka The Divi Teacher)

      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.

      Reply
  28. Ben

    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

    Reply
  29. Allegra

    Hi! this snippet was really useful. It shows in random order all the post…Is it possible to exclude a category?

    Reply
  30. Marc Ramos

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

    Reply
    • Hemant Gaba

      Hey Marc,

      Will check the code on our end and get back to you on this.

      Reply
  31. Fredrik

    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

    Reply
  32. Lala Loveyou

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

    Reply
  33. Chris H

    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.

    Reply
  34. Michel

    Hello, Nelson

    How to configure the blog module to display the most viewed posts?

    Reply
    • Nelson Lee Miller (aka The Divi Teacher)

      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.

      Reply
  35. Ronald Weijman

    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?

    Reply

Submit a Comment

Your email address will not be published.

Recent Posts

0

Your Cart