0

My posts are in two categories - one that defines their type eg. article or review and the other defines the topic - tablets,smartphones,smartwatches, etc.

How can I get the current post's category ID for its topic - i.e. whether it's a tablet, smartphone or smartwatch.

If I do get_the_category( $post->ID ); I suppose it'll return all categories the post is in, but how do I filter out the article or review categories out?

Example: Post ABC is in categories article with ID=1 and tablets with ID=10 how can I extract on the tablets ID=10 category without the article one?

0

You can simply loop over the categories that you don't need. get_the_category() return an array of categories that the post belongs to.

With that in mind, you can do the following: (Just remember to change 21 and 41 to your desired ID's)

$categories = get_the_category();
foreach ( $categories as $category ) {
    if( 21 ===  $category->cat_ID || 41 === $category->cat_ID ) {
        continue;
    }else{
        echo $category->cat_ID . '</br>';
    }
}

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.