Make WordPress Core

Opened 14 years ago

Closed 13 years ago

#14625 closed defect (bug) (duplicate)

Function get_the_category doesn't work as expected with custom post types

Reported by: bjdj's profile 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)

#1 @bjdj
14 years ago

For clarification, in the sentence 'If a category has no posts assigned to it', posts are posts with post_type = post.

#2 @scribu
14 years ago

Please paste the relevant code block.

#3 @scribu
14 years ago

  • Keywords reporter-feedback added; get_the_category() removed

...that is causing the problem.

#4 @bjdj
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 @scribu
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 @bjdj
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 @nacin
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 @mfields
13 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:

  1. Registered post_type 'resource' (init, 0).
  2. Registered 'category' as a taxonomy for object type 'resource' (init, 1).
  3. Created a resource and assigned it to category 94.
  4. Visited /?cat=94 recieved a 404. No post were displayed.
  5. Created a post and assigned it to category 94.
  6. Visited /?cat=94 Only the post was displayed - no resource.
  7. Assigned the post to all other available categories.
  8. Visited /?cat=94 Only the post was displayed - no resource.

#9 @nacin
13 years ago

  • Resolution set to duplicate
  • Status changed from reopened to closed

On trunk, you'll now see custom post types in category/tag indexes.

Note: See TracTickets for help on using tickets.