pre get posts - Automatically applying a pre_get_posts filter for child categories only

admin2025-06-02  1

I have a category named "Research" that has a growing list of child categories.

This research category has a special template category-research.php that shows a list of its children instead of the normal posts loop. So, it behaves like a Table of Contents, in a way.

The child categories of Research are many, and they are all using the category.php template. For that, I have modified the main query as such:

// Custom query for research sub-categories
function custom_query_for_research_subcategories($query) {
    if( !is_admin() && $query->is_category( array('7', '9', '11', '12', '15' , '17' , '18' , '21' )) ) {
      $qobj = get_queried_object();
      if( isset($qobj->taxonomy) && 'category' == $qobj->taxonomy ) {
        $query->set( 'orderby', 'title' );
        $query->set( 'order', 'asc' );
      }
    }
}
add_filter( 'pre_get_posts', 'custom_query_for_research_subcategories' );

This is because I want all posts in sub-categories to be ordered alphabetically by their title.

It works perfectly, no problems whatsoever. You open the research page and see all child categories. You click on any one of those and you see a list of all posts under that child category, organized alphabetically by title.

BUT - I am concerned because if I should create a new child category of Research, then it won't be affected by this filter unless I remember to edit the array in functions.php to include the new child category ID. That would not be practical in the long run.

So, my question is, can this array or the code above be modified so that it automatically applies to all child categories of Research?

I have a category named "Research" that has a growing list of child categories.

This research category has a special template category-research.php that shows a list of its children instead of the normal posts loop. So, it behaves like a Table of Contents, in a way.

The child categories of Research are many, and they are all using the category.php template. For that, I have modified the main query as such:

// Custom query for research sub-categories
function custom_query_for_research_subcategories($query) {
    if( !is_admin() && $query->is_category( array('7', '9', '11', '12', '15' , '17' , '18' , '21' )) ) {
      $qobj = get_queried_object();
      if( isset($qobj->taxonomy) && 'category' == $qobj->taxonomy ) {
        $query->set( 'orderby', 'title' );
        $query->set( 'order', 'asc' );
      }
    }
}
add_filter( 'pre_get_posts', 'custom_query_for_research_subcategories' );

This is because I want all posts in sub-categories to be ordered alphabetically by their title.

It works perfectly, no problems whatsoever. You open the research page and see all child categories. You click on any one of those and you see a list of all posts under that child category, organized alphabetically by title.

BUT - I am concerned because if I should create a new child category of Research, then it won't be affected by this filter unless I remember to edit the array in functions.php to include the new child category ID. That would not be practical in the long run.

So, my question is, can this array or the code above be modified so that it automatically applies to all child categories of Research?

Share Improve this question asked Mar 17, 2019 at 16:26 jsmodjsmod 5013 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

Try this:

EDIT: added short circuit return statements to avoid running unnecessary code on admin pages or other queries (e.g. nav menu items)

// Custom query for research sub-categories
function custom_query_for_research_subcategories($query) {
    if (is_admin() || !$query->is_main_query()) {
        return $query;
    }
    $qobj = get_queried_object();
    if (isset($qobj->taxonomy) && 'category' == $qobj->taxonomy ) {
        $term = get_term($qobj->term_id);
        if (!is_wp_error($term) && $term->parent === '45') { // replace 45 with whatever the research parent category is
            $query->set('orderby', 'title');
            $query->set('order', 'asc');
        }
    }
}
add_filter( 'pre_get_posts', 'custom_query_for_research_subcategories' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748796986a313796.html

最新回复(0)