Make WordPress Core

Opened 4 years ago

Last modified 17 months ago

#53319 new enhancement

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

Reported by: anonymized_14808221's profile anonymized_14808221 Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Taxonomy Keywords: has-patch 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 anonymized_14808221 4 years 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 anonymized_14808221 4 years ago.
category-template-1.php
53319.diff (1.6 KB) - added by jnylen0 4 years ago.
I converted the changes in this ticket into a patch file.

Download all attachments as: .zip

Change History (18)

@anonymized_14808221
4 years ago

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

#1 @joyously
4 years ago

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

#2 @anonymized_14808221
4 years 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: @anonymized_14808221
4 years 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.

@anonymized_14808221
4 years ago

category-template-1.php

#4 in reply to: ↑ 3 ; follow-up: @jnylen0
4 years 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
4 years 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.


4 years ago

#6 follow-up: @mukesh27
4 years 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
4 years 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
4 years ago

  • Milestone changed from Awaiting Review to 5.9

#9 in reply to: ↑ 6 @jnylen0
4 years 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
4 years 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.

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


3 years ago

#12 @hellofromTonya
3 years ago

  • Milestone changed from 5.9 to Future Release

Today is 5.9 Feature Freeze. As there's more to do, punting this one to the next cycle. But as the next cycle isn't yet available in the milestones, it's set to Future Release. Sorry about that. Once available, please feel free to move into 6.0.

This ticket was mentioned in PR #1859 on WordPress/wordpress-develop by TukuToi.


3 years ago
#13

  • Keywords needs-refresh removed

Order hierarchy for term names. If true, the terms returned are ordered from parent to child. If false, the terms returned are ordered from child to parent. Default is true. Accepts bool false|true.

Trac ticket: [](https://core.trac.wordpress.org/ticket/53319)

#14 @anonymized_14808221
3 years ago

@hellofromTonya thanks for the reminder, I somehow forgot about this ticket, honestly...

I PR'd the changes with adequate amendments which should finalise the missing work to GIT, and if I don't forget I will revisit here when 6.0 is available, so we can (maybe) revisit it then.

#15 @anonymized_14808221
17 months ago

@hellofromTonya how to move this ticket back into a state where it gets done in the next release? We are way past 6.0 now.

Note: See TracTickets for help on using tickets.