Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need to change the number of posts queried for specific categories 'Recipes' or 'Product Reviews'. The below code works great for this, I have it in functions.php:

function change_number_of_posts($query) {
    if ($query->is_category && ( $query->is_category('recipes') || $query->is_category('product-reviews') ) )
    $query->query_vars['posts_per_page'] = -1; // Show all posts
    return $query; // Return our modified query variables
}

add_filter('pre_get_posts', 'change_number_of_posts'); // Hook our custom function onto the request filter

The problem is that the 'Product Reviews' category is a parent category, so the above code does not work when one of its child categories is being queried.

I can use the 'cat_is_ancestor_of()' function in my loop to do what I need to with these subcategories, but I cannot get it to work in functions.php and am thinking it cannot be called from there.

I basically want to say "If the category is 'recipes' or 'product-reviews' or a child of 'product-reviews'..." from my functions.php file.

I know I can hard code all the subcategories, but I don't want to have to edit the php every time I add a subcategory of Product Reviews.

I have spent hours trying to find a solution, any help is greatly appreciated!!

share|improve this question
    
You should be able to use cat_is_ancestor_of in your functions.php, in particular in your change_number_of_posts function. Could you post some code to show how you are using it? –  Stephen Harris Mar 18 '12 at 20:43
    
That is great that cat_is_ancestor_of can be used. I guess then I don't know which argument to pass for the second variable. 'code' if ($query->is_category('recipes') || $query->is_category('product-reviews') || cat_is_ancestor_of(6,WHAT-GOES-HERE?) 'code' Thanks! (so sorry, I don't know how to format this properly!) –  Angie Mar 18 '12 at 20:57
add comment

1 Answer 1

up vote 0 down vote accepted

The function cat_is_ancestor_of takes two arguments (two category IDs). To check if category of ID 61 is ancestor of category with 54:

 cat_is_ancestor_of(54,61);

I'm not sure if there is a better way of doing this, but you can get the current category's ID with the get_queried_object() and then check this an ancestor of the category with some ID (6 in this example):.

function change_number_of_posts($query) {
    if ($query->is_category){
         $cat = get_queried_object();
         $cat_id = (int) $cat->term_id;

         if( $query->is_category('recipes') || $query->is_category('product-reviews')){
              $query->query_vars['posts_per_page'] = -1; 
         }elseif(cat_is_ancestor_of(6,$cat_id)){
              $query->query_vars['posts_per_page'] = -1; 
         }
    }

    return $query;
}

add_filter('pre_get_posts', 'change_number_of_posts'); 

Untested.

(I've left out any checking of errors (e.g. in case the category doesn't exist)).

share|improve this answer
    
That's it!! I did not know how to extract the Category ID of the current query as an int. Thank you so very much. :) –  Angie Mar 18 '12 at 22:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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