WordPress.org

Make WordPress Core

Opened 2 months ago

Last modified 8 weeks ago

#53319 new enhancement

Add support for a "reverse" argument in get_term_parents_list() function

Reported by: bedas Owned by:
Milestone: 5.9 Priority: normal
Severity: normal Version:
Component: Taxonomy Keywords: has-patch needs-refresh needs-unit-tests
Focuses: Cc:

Description

Please bare with me, I probably do everything wrong since it is my first Trac Ticket involving code.

---

Recently I used get_term_parents_list() to create a list of hierarchical terms.
I noticed that by default the function outputs the terms like so:
Parent Category/Child Category/Grandchild Category

I could not find any argument or way to invert that order, with the goal to display the terms like so:
Grandchild Category/Child Category/Parent Category

This is useful for example if we have Geographic zones and the utmost parent is a Country, the utmost child an area. In that case we may want to display Area/Region/Country which looks like an address structure rather than the opposed which is the default

I don't think it should be too hard adding this new argument to the function.
I have uploaded a file with the modified function and updated code comment to the ticket.

I also tested my changes on a existing install and can confirm this introduced no unexpected issues.

It would be awesome to see if we can add this tiny change in future release.

Attachments (3)

category-template.php (2.1 KB) - added by bedas 2 months ago.
Updated method get_term_parents_list that is registered in category-template.php currently on line 1377
category-template.2.php (2.1 KB) - added by bedas 2 months ago.
category-template-1.php
53319.diff (1.6 KB) - added by jnylen0 8 weeks ago.
I converted the changes in this ticket into a patch file.

Download all attachments as: .zip

Change History (13)

@bedas
2 months ago

Updated method get_term_parents_list that is registered in category-template.php currently on line 1377

#1 @joyously
2 months ago

Why add a parameter when it can be filtered with get_ancestors?

#2 @bedas
2 months ago

@joyously afaik, the get_ancestors function returns An array of IDs of ancestors from lowest to highest in the hierarchy.

Thus, Because the get_ancestors doesn’t provide the current/start term and that term is added within get_term_parents_list only and it’s added at the begin of the terms list, if you invert the array in get_ancestors you would get Child Category/Parent Category/Grandchild Category as a result of get_term_parents_list

Maybe I’m wrong but this is what I saw in the code

#3 follow-up: @bedas
2 months ago

Do we really need to down load and upload the proposed code change for each and every single edit?

---

That said, changed terminology from inverse to reverse and added check for boolean thanks @joyously for the heads up.

Attached new file to ticket.

@bedas
2 months ago

category-template-1.php

#4 in reply to: ↑ 3 ; follow-up: @jnylen0
8 weeks ago

Replying to bedas:

Do we really need to down load and upload the proposed code change for each and every single edit?

You shouldn't need to download the code change, only upload a new patch or diff file. As described here: https://make.wordpress.org/core/handbook/tutorials/working-with-patches/

@jnylen0
8 weeks ago

I converted the changes in this ticket into a patch file.

This ticket was mentioned in Slack in #core by jnylen. View the logs.


8 weeks ago

#6 follow-up: @mukesh27
8 weeks ago

  • Component changed from General to Taxonomy
  • Keywords needs-refresh added
  • Version trunk deleted

@jnylen0 is 53319.diff working for you?

@type bool   $reverse   Wether to reverse the hierarchical order of the Terms on output. Default true.

Also, please change the Whether spell.

#7 in reply to: ↑ 4 @SergeyBiryukov
8 weeks ago

Replying to jnylen0:

Replying to bedas:

Do we really need to down load and upload the proposed code change for each and every single edit?

You shouldn't need to download the code change, only upload a new patch or diff file. As described here: https://make.wordpress.org/core/handbook/tutorials/working-with-patches/

You can also submit a pull request on GitHub. Just make sure to include the ticket URL in the PR description, and it will be automatically linked to the ticket.

#8 @SergeyBiryukov
8 weeks ago

  • Milestone changed from Awaiting Review to 5.9

#9 in reply to: ↑ 6 @jnylen0
8 weeks ago

  • Keywords needs-unit-tests added

Replying to mukesh27:

@jnylen0 is 53319.diff working for you?

I've not tested this but the author reports that it is working as intended on multiple sites. A unit test or two would be good to confirm correct operation now and in the future.

Also, not sure if this needs an @since annotation for the new parameter?

#10 @jnylen0
8 weeks ago

Also, I think calling this argument parents_first (default true) or children_first (default false) would be more descriptive. reverse isn't immediately clear about what it is reversing until you look in the code and see that it's related to the use of the array_reverse function.

Note: See TracTickets for help on using tickets.