Opened 14 years ago
Closed 14 years ago
#14625 closed defect (bug) (duplicate)
Function get_the_category doesn't work as expected with custom post types
Reported by: | bjdj | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | minor | Version: | 3.0.1 |
Component: | General | Keywords: | reporter-feedback |
Focuses: | Cc: |
Description
On a category page, the function 'get_the_category()' does not work as expected, combined with custom post types.
If a category has no posts assigned to it, 'get_the_category()' will return an empty array (probably because the global $post variable is empty as well?). If your WordPress installation only uses custom post types (like mine), and never uses the post_type 'post', this could be a problem.
I think I found an easy, not very beautiful, workaround for this problem; assign a post, which has post_type 'post', to all categories. That way, 'get_the_category()' returns an array with information.
Change History (9)
#3
@
14 years ago
- Keywords reporter-feedback added; get_the_category() removed
...that is causing the problem.
#4
@
14 years ago
On my category pages (http://example.com/?cat=X) I have the following block of code:
<?php
global $post;
$gtc_test = get_the_category();
print_r($gtc_test);
print_r($post);
?>
Both arrays are empty on category pages which have no posts with posttype 'post' assigned e.g. if a category has posts with posttype 'movie' both arrays will stay empty.
If a category does have a post with posttype 'post' both arrays will be filled with data and it is possible to retrieve the data e.g. the 'cat_ID' from the get_the_category() array.
I hope this answers your question
#5
@
14 years ago
- Milestone Awaiting Review deleted
- Resolution set to worksforme
- Status changed from new to closed
I'm assuming you want to get to the X category. In that case you should use $wp_query->get_queried_object()
#6
@
14 years ago
- Resolution worksforme deleted
- Status changed from closed to reopened
Again, I got some 'unexpected' behaviour on the category page. I don't know if this behaviour is a bug or not, so that's the reason I am posting this at this ticket.
Scenario:
You print $post (using print_r($post) for example) on a category page. The array $post is filled with data from the last post in the category. To be more precise, the last post that has post_type = post.
I notice two things:
1) Should the $post variable be set at all on the category pages?
2) If so, why does it contain the latest post with post_type = post. It seems more logical to me to fill it with the latest post, of any post_type.
Thanks in advance.
#7
@
14 years ago
By default, category/tag index pages will only show post_type = post. You'll need to hook into the WP_Query or WP objects to display additional post types.
#8
@
14 years ago
I did a bit of testing with this and WordPress appears to be working as intended AFAIK. Here's what I did on 3.0.1 using TwentyTen:
- Registered post_type 'resource' (init, 0).
- Registered 'category' as a taxonomy for object type 'resource' (init, 1).
- Created a resource and assigned it to category 94.
- Visited /?cat=94 recieved a 404. No post were displayed.
- Created a post and assigned it to category 94.
- Visited /?cat=94 Only the post was displayed - no resource.
- Assigned the post to all other available categories.
- Visited /?cat=94 Only the post was displayed - no resource.
For clarification, in the sentence 'If a category has no posts assigned to it', posts are posts with post_type = post.