0

I am trying to check whether a post is in categories X, Y, Z (including the subcategories within them).

This is the piece of code I have tried in my single.php:

if ( in_category(array( '1', '2', '3', ) ) {
                    echo "This post is in the selected categories or their subcategories";
                              }
                else {
                echo "this post inst in the categories or the subs.";
                          }

I have also tried using the post_is_in_descendant_category (included the function from the codex above my code) but it still wont display true. Including the function This is my code I used on test 2#:

    if ( in_category(array( '1', '2', '3', ) ) || post_is_in_descendant_category( array( '1', '2', '3', ) ) ) {
                    echo "This post is in the selected categories or their subcategories";
                              }
                else {
                echo "this post inst in the categories or the subs.";
                          }
1

post_is_in_descendant_category is not a WP function. If you don't define it, it doesn't exist. I think that you have read the docs for in_category() function and have taken post_is_in_descendant_category from the example without read it completely. Add this code to functions.php or in a plugin:

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;
    }
}

Now post_is_in_descendant_category() is defined and you can use it. Remember to use in_category() or post_is_in_descendant_category() inside The loop, if not, you need to pass the post ID or object to check.

NOTE: You are using array( '1', '2', '3', ). The values inside the array are strings; I think you refer to categories with ID 1, 2 and 3. To pass cateogories IDs you have pass the values as integer: array( 1, 2, 3 ). Also note that post_is_in_descendant_category only accepts category IDs as integer values.

  • Thank you forgot to mention I had added that function already but still it shows as if the post isn't in the category or any of the subcategories of the 1, 2, 3 . Do you see any error in my code? I cant seem to find whats wrong with it. – Jack Stanson Jul 26 '14 at 14:49
  • Are the code used inside The loop? – cybmeta Jul 26 '14 at 14:55
  • Im beginner with wordpress, but I think so. Above that code is: if ( have_posts() ){ while (have_posts()){ the_post(); ...etc – Jack Stanson Jul 26 '14 at 15:17
  • Your code must be inside the while loop after the_post(); and before closing the while loop. If not, you must pass a post ID or post object to the in_category() and post_is_in_descendant_category() functions. – cybmeta Jul 26 '14 at 19:09
  • I have found out why it wasnt working the first field "in_category(array( 'fruits', 'vegetables', 'shoes', )" needs to be with the categories names and not IDs now it works fine! I have updated the answer with additional info and explanation. – Jack Stanson Jul 27 '14 at 18:12

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.