0

I'm building out an index page of posts that are grouped under multiple categories, but those categories are all under the same umbrella parent category.

I'd like to have these posts displayed alphabetically by default.

I've found functions that work to change the post order globally on my site, but I'd like to find a function that works to ONLY sort posts alphabetically based on either the post ID of this index page or the parent category ID.

I'm using a Divi child theme if that matters for anything.

2 Answers 2

0

You can use the following code snippet to sort posts alphabetically based on a specific parent category in WordPress:

<?php 
$parent_cat = 'Parent Category Name'; 
$parent_cat_id = get_cat_ID($parent_cat); 
$args = array(
 'category__in' => array($parent_cat_id),
 'orderby' => 'title', 
 'order' => 'ASC', 
 'posts_per_page' => -1
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
 while ( $query->have_posts() ) {
   $query->the_post();
   the_title();
   echo '<br>';
 }
 wp_reset_postdata();
} else {
 // no posts found
}
?>

In the code, replace "Parent Category Name" with the actual name of the parent category you want to sort posts from. The code will retrieve all the posts under this category, sort them alphabetically based on the post title, and display the title of each post.

1
  • Thanks for this! I'll check it out. In the meantime I found another way to make things work the way I wanted to by creating a child file for the 'blog.php' module, and added a section in that to sort alphabetically based on the page ID of the page that contains this index and it works perfectly!
    – jzn110
    Commented Feb 7, 2023 at 12:54
0

You can use the pre_get_posts action hook to modify the query that retrieves the posts, and sort them alphabetically based on a specific parent category. Here's an example:

function sort_posts_alphabetically_by_parent_category( $query ) {
    if ( is_admin() || ! $query->is_main_query() ) {
        return;
    }
    
    if ( is_category( 'your-parent-category-slug' ) ) {
        $query->set( 'orderby', 'title' );
        $query->set( 'order', 'ASC' );
    }
}
add_action( 'pre_get_posts', 'sort_posts_alphabetically_by_parent_category' );

Replace 'your-parent-category-slug' with the actual slug of your parent category. This function will sort the posts alphabetically based on their title, only when the parent category archive page is being displayed.

1
  • Thanks! I'll try this as well. I found another way to make things work the way I wanted to by creating a child file for the 'blog.php' module, and added a section in that to sort alphabetically based on the page ID of the page that contains this index and it works perfectly!
    – jzn110
    Commented Feb 7, 2023 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.