WordPress.org

Make WordPress Core

Opened 6 years ago

Last modified 4 days ago

#17069 reopened enhancement

missing get_term_parents() function

Reported by: thomask Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.1
Component: Taxonomy Keywords: has-patch good-first-bug has-unit-tests
Focuses: Cc:

Description

we got get_category_parents (http://core.trac.wordpress.org/browser/tags/3.1/wp-includes/category-template.php#L30)
which use $parent = &get_category( $id ); limited to 'category' taxonomy, but there is nothing similar to other taxonomies. IMO we should rewrite this function to be generic and use it as get_term_parents function, the existing get_category_parents would use that get_term_parents, so something like (sorry, i do not know how to use diff) this:

42	function get_term_parents( $id, $taxonomy, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
43	        $chain = '';
44	        $parent = &get_term( $id, $taxonomy );
45	        if ( is_wp_error( $parent ) )
46	                return $parent;
47	
48	        if ( $nicename )
49	                $name = $parent->slug;
50	        else
51	                $name = $parent->name;
52	
53	        if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
54	                $visited[] = $parent->parent;
55	                $chain .= get_term_parents( $parent->parent, $link, $separator, $nicename, $visited );
56	        }
57	
58	        if ( $link )
59	                $chain .= '<a href="' . get_term_link( $parent->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
60	        else
61	                $chain .= $name.$separator;
62	        return $chain;
63	}

and

function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
   return get_term_parents ($id,'category',$separator,$nicename,$visited);
}

Attachments (3)

category-template.patch (1.6 KB) - added by rafaehlers 5 years ago.
the patch with the new function
17069.patch (2.7 KB) - added by SergeyBiryukov 5 years ago.
17069.1.patch (7.9 KB) - added by keesiemeijer 4 days ago.
Add get_term_parents_list() function and unit tests

Download all attachments as: .zip

Change History (20)

#1 @scribu
6 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

We have something even better than get_term_parents(): get_ancestors().

See #12443

#2 @scribu
6 years ago

Granted, it returns a raw array, not a list of links.

#3 @scribu
6 years ago

  • Keywords needs-testing has-patch removed
  • Milestone set to Future Release
  • Resolution duplicate deleted
  • Status changed from closed to reopened

I guess I was a bit hasty.

We could add a get_term_parents() template tag that uses get_ancestors() internally.

For creating patches: http://core.trac.wordpress.org/#HowtoSubmitPatches

#4 @rafaehlers
6 years ago

thomask: you missed on line 55 the argument $taxonomy

actual: $chain .= get_term_parents( $parent->parent, $link, $separator, $nicename, $visited );

should be: $chain .= get_term_parents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited );

#5 @rafaehlers
6 years ago

  • Cc rafaehlers added

another argument missing on line 59 on function get_term_link, missed $taxonomy and use $parent->slug instead.

actual: $chain .= '<a href="' . get_term_link( $parent->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;

should be: $chain .= '<a href="' . get_term_link( '''$parent->slug, $taxonomy''') . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;

Last edited 5 years ago by scribu (previous) (diff)

@rafaehlers
5 years ago

the patch with the new function

#6 @SergeyBiryukov
5 years ago

  • Keywords has-patch added

#7 @scribu
5 years ago

  • Keywords needs-patch added; has-patch removed

As I said, get_term_parents() should use get_ancestors(). And then, get_category_parents() should be made to use get_term_parents().

#8 @SergeyBiryukov
5 years ago

  • Keywords has-patch added; needs-patch removed

Right, updated the patch.

Looks like $visited parameter can be deprecated (it isn't documented in Codex anyway).

#9 @SergeyBiryukov
5 years ago

Related: #7030 (the latest patches overlap a lot)

#10 @alexvorn2
3 years ago

so closing? I would like this patch to be included in the core.

#11 @wonderboymusic
3 years ago

  • Keywords needs-refresh added

#12 @boonebgorges
19 months ago

  • Keywords needs-unit-tests added

#13 @johnbillion
2 months ago

#30415 was marked as a duplicate.

#14 @johnbillion
2 months ago

#35886 was marked as a duplicate.

#15 @johnbillion
2 months ago

  • Keywords good-first-bug added

@keesiemeijer
4 days ago

Add get_term_parents_list() function and unit tests

#16 @keesiemeijer
4 days ago

  • Keywords has-unit-tests added; needs-unit-tests removed

In patch 17069.1.patch I've renamed the function to get_term_parents_list to make it more clear what it returns. The $visited parameter is deprecated in get_category_parents() as it was only used to avoid duplicates from the recursive calls to itself.

#17 @keesiemeijer
4 days ago

  • Keywords needs-refresh removed
Note: See TracTickets for help on using tickets.