$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>pre get posts - Automatically applying a pre_get_posts filter for child categories only|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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' );
Post a comment

comment list (0)

  1. No comments so far