I created a custom function in functions.php to check if post_is_in_descendant_category and I'm controlling it from a plugin which is returning true is a post is in descendant category, it is working well

But it's not working in my home.php, the strange thing is that when I check if post_is_in_descendant_category for any post in home.php result stuck to true

I want to know the reason for that and how to fix it

my code for post_is_in_descendant_category

if ( ! function_exists( 'post_is_in_descendant_category' ) ) {
    function post_is_in_descendant_category( $cats, $_post = null ) {
        foreach ( (array) $cats as $cat ) {
            // get_term_children() accepts integer ID only
            $descendants = get_term_children( (int) $cat, 'category' );
            if ( $descendants && in_category( $descendants, $_post ) )
                return true;
        return false;

my plugin code:

function check_category(){  
    global $rtlDir;
    if ( $category_to_check = get_term_by( 'slug', 'category-3', 'category' )){
            $rtlDir = true;
            $rtlDir = false;


1 Answer 1


home.php is an archive of posts, and when you view home.php and function in plugin run, (on 'wp' hook) there is no current post.

So the function post_is_in_descendant_category call in_category($descendants, null) that can never return true if there is no current post.

For this reason, you should run the plugin function on 'the_post' action hook, if you want it runs for every post in the archive, something like:

add_filter("the_post", "check_category");

function check_category($post) {
  // here your code
  global $rtlDir;
  if ( $category_to_check = get_term_by( 'slug', 'category-3', 'category' ) ){
    if( post_is_in_descendant_category($category_to_check->term_id, $post) ){
        $rtlDir = true;
    } else {
        $rtlDir = false;

This should work, but don't seems a good solution: this run an additional db query for every post...

If you explain why you run this code, and what you want to obtain probably is possible to find a more performant solution.

  • Thank you for reply. I think I can't use the_post filter because I'm doing some operations for the post title class <h1 class="entry-title"> depends on the post descendant category Sep 17, 2013 at 17:40
  • @usamasulaiman the_post action is fired when you call the_post() function inside the loop. The post title and its class are printed after that, so what's the problem?
    – gmazzap
    Sep 17, 2013 at 17:45
  • @ G.M. I'll I know that point, mmmm ok you show me another way now, I'm thinking to do similar plugin below the first one and add it to the_post filter but adding if(is_home()) condition Sep 18, 2013 at 8:11
  • @ G.M I tried the_post but I got same result and it stuck to true what ever the post category is. Sep 18, 2013 at 18:03

Your Answer

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

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