WordPress.org

Make WordPress Core

Opened 8 years ago

Closed 6 years ago

Last modified 6 years ago

#22471 closed defect (bug) (worksforme)

get_category_parents problem with sub-categories (all WP Versions - to 3.5 beta)

Reported by: wpweaver Owned by:
Milestone: Priority: normal
Severity: normal Version: 1.2
Component: Taxonomy Keywords: close
Focuses: Cc:

Description

get_category_parents does not always show full hierarchy when showing subcategories. Seems to be related to alphabetical order?

This example demonstrates the problem. Use the same words & spelling for the Category Name - because it matters to the outcome. Parent category: "Photography" and give it 2 Child categories: "Event" and "Wedding". Now create 2 posts, Post #1: Categories "Photography" & "Event", Post #2: Categories "Photography" & "Wedding".

Now use a theme with a breadcrumbs function that uses get_category_parents to generate the breadcrumbs. Yoast SEO plugin will demonstrate the problem, for example, as will the Weaver II theme.

You will get the following output when displaying the single page view of each post:

The Photography->Event post gives this correct breadcrumb:

Home→Photography→Event→Photo-Event

The other Photography>Wedding gives this incorrect breadcrumb:

Home→Photography→Photo-wedding

note the missing Wedding in the breadcrumb.

This problem shows on the latest WP 3.5 beta, too.

Change History (7)

#1 @c3mdigital
8 years ago

  • Version changed from trunk to 1.2

#2 @c3mdigital
8 years ago

  • Keywords close added
  • Resolution set to worksforme
  • Status changed from new to closed

get_category_parents works as expected. It retrieves a child categories parents. Your issue is how the category id that gets passed to the function is retrieved. If you are using get_the_category() the term array returned will be ordered alphabetically ( see wp_get_object_terms(); )

add_filter( 'the_content', 'test_get_cat_parents' );
function test_get_cat_parents( $content ) {
    $cats = get_the_category();
    
    return $content. '<br>' . get_category_parents( $cats[0]->term_id );
 }

This will return only photography for post 2. If the array index is changed from 0 to 1 then then it will return photography and wedding because the id passed to get_category_parents will be the actual child term.

#3 @helenyhou
8 years ago

  • Milestone Awaiting Review deleted

#4 @mcleod@…
6 years ago

  • Resolution worksforme deleted
  • Status changed from closed to reopened

This issue still exists in wp 3.9.2

This ticket was mentioned in IRC in #wordpress-dev by bartmcleod. View the logs.


6 years ago

#6 @mcleod@…
6 years ago

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

A second observation is that this might be related to get_the_category(). This gives a list of categories, not necessarily in hierarchical order. Somewhere in the codex or elsewhere (need to research where that was), it is stated that the last child in the hierarchy is the first category returned from get_the_category(). This is not true.

Example:

$categories = get_the_category();
$category = $categories[0];
echo get_category_parents($category, ' - ');

This gives unexpected results as described in this very ticket, if $category is not the last child in the hierarchical chain.

If the $category argument is the last child, the results are as expected. So I was probably wrong to re-open this ticket and I will close it again until I have more results.

Last edited 6 years ago by SergeyBiryukov (previous) (diff)

#7 @SergeyBiryukov
6 years ago

  • Component changed from General to Taxonomy
Note: See TracTickets for help on using tickets.