admin管理员组

文章数量:1125477

What I am doing is, I have to check if the post exists in any of the categories or not. I have tried below two codes but it's not working for me.

This is the first code

foreach ($scats as $key => $value) {
//echo $value->name;
if (is_object_in_term(get_the_ID(),$value->term_id)) {
echo "Found in category: " . esc_html($value->name);
        }
}

This is the second code

foreach ($scats as $category) {
    if (in_category($category->slug, get_the_ID())) {
        echo "Found in category: " . esc_html($category->name);
    }
}

I am not getting the output. Would you help me out with this?

Below is the $scats array

Array
(
    [0] => WP_Term Object
        (
            [term_id] => 700
            [name] => Change
            [slug] => change
            [term_group] => 0
            [term_taxonomy_id] => 700
            [taxonomy] => solution_cats
            [description] => 
            [parent] => 0
            [count] => 6
            [filter] => raw
            [cat_ID] => 700
            [category_count] => 6
            [category_description] => 
            [cat_name] => Change
            [category_nicename] => change
            [category_parent] => 0
        )

    [1] => WP_Term Object
        (
            [term_id] => 701
            [name] => Game
            [slug] => game
            [term_group] => 0
            [term_taxonomy_id] => 701
            [taxonomy] => solution_cats
            [description] => 
            [parent] => 0
            [count] => 9
            [filter] => raw
            [cat_ID] => 701
            [category_count] => 9
            [category_description] => 
            [cat_name] => Game
            [category_nicename] => game
            [category_parent] => 0
        )

)

What I am doing is, I have to check if the post exists in any of the categories or not. I have tried below two codes but it's not working for me.

This is the first code

foreach ($scats as $key => $value) {
//echo $value->name;
if (is_object_in_term(get_the_ID(),$value->term_id)) {
echo "Found in category: " . esc_html($value->name);
        }
}

This is the second code

foreach ($scats as $category) {
    if (in_category($category->slug, get_the_ID())) {
        echo "Found in category: " . esc_html($category->name);
    }
}

I am not getting the output. Would you help me out with this?

Below is the $scats array

Array
(
    [0] => WP_Term Object
        (
            [term_id] => 700
            [name] => Change
            [slug] => change
            [term_group] => 0
            [term_taxonomy_id] => 700
            [taxonomy] => solution_cats
            [description] => 
            [parent] => 0
            [count] => 6
            [filter] => raw
            [cat_ID] => 700
            [category_count] => 6
            [category_description] => 
            [cat_name] => Change
            [category_nicename] => change
            [category_parent] => 0
        )

    [1] => WP_Term Object
        (
            [term_id] => 701
            [name] => Game
            [slug] => game
            [term_group] => 0
            [term_taxonomy_id] => 701
            [taxonomy] => solution_cats
            [description] => 
            [parent] => 0
            [count] => 9
            [filter] => raw
            [cat_ID] => 701
            [category_count] => 9
            [category_description] => 
            [cat_name] => Game
            [category_nicename] => game
            [category_parent] => 0
        )

)
Share Improve this question edited Feb 1, 2024 at 8:32 Naren Verma asked Feb 1, 2024 at 5:40 Naren VermaNaren Verma 2491 gold badge6 silver badges19 bronze badges 2
  • I could tell why the 1st code didn't work, but what is $scats, i.e. how are you getting the categories? Can you add the code to your post/question, and are you sure get_the_ID() is giving the correct post ID? – Sally CJ Commented Feb 1, 2024 at 7:46
  • 1 @SallyCJ, I have added the $scats code in the question. The first code shows all the category names and I want if the post exists in the category then show that category . Yes, get_the_ID() is showing the correct Id number. – Naren Verma Commented Feb 1, 2024 at 8:34
Add a comment  | 

1 Answer 1

Reset to default 2

Since you are doing this for a custom taxonomy (solution_cats), then you should instead use has_term() and not in_category() which works only with the default/core category taxonomy. So try this with your second code:

  • has_term( $category->slug, $category->taxonomy, get_the_ID() )

is_object_in_term() can also be used, but your syntax was not correct – the 2nd parameter is actually the term's taxonomy and not the term ID/name/slug (which should instead be the 3rd parameter).

  • The correct syntax: is_object_in_term(get_the_ID(),$value->taxonomy,$value->term_id)
  • But you used this: is_object_in_term(get_the_ID(),$value->term_id)

本文标签: pluginsHow to check if the post exists in any of the categories