0

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?

2

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' );
  • I tried this out of curiosity (since I already got an answer), but this did not work for me. – jsmod Mar 17 at 17:20
  • 1
    Maybe double equals instead of triple for the parent check. In my phone right now and can't debug for you – mrben522 Mar 17 at 20:02
  • It works with double equals. Thank you! But what Is the difference between this version and the one @Qaisar provided? Is it just a matter of conciseness? If both work, which is the best one to use? – jsmod Mar 18 at 3:10
  • (Side question: can we not accept two answers here since both worked? Because I tried but it doesn't seem to work...) – jsmod Mar 18 at 3:11
  • 1
    My answer should be a little more performant as get_queried_object will return the queried object that should already exist on the WP_Query object before trying to create one while the other answer is running a database query (get_categories) before checking if it needs to run on every single query. To make mine even more performant you could add more short circuit statements at the top, I'll add those in an edit now – mrben522 Mar 18 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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