Can’t get category slug programmatically
-
I have 3 posts. All of them are attached to 3 categories with slugs: “game-updates-patches”, “patch”, “news”. Category “patch” is a subcategory. The parent is “game-updates-patches”.
Everything is okay in admin section:
Now I want to check whether my post is attached to “patch” category or not.
I use this code to do so:
$posts_array = get_posts([ 'category' => 'game-updates-patches' ]); foreach($posts_array as $post) { /* Checking! */ if(has_category('patch', $post->ID)) { some_stuff... } }
The problem is that it can’t detect the third post! This condition works fine for last two posts only!
Here is the image of categories of the third post:
As you can see “Патч” (which slug is patch) is checked. However, this post fails condition mentioned above. Moreover, I printed all categories of this third post using get_the_category() function and it showed only “news” category.
- The topic ‘Can’t get category slug programmatically’ is closed to new replies.