WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 3 years ago

#35925 closed enhancement (fixed)

Filter for wp_get_object_terms() arguments

Reported by: enrico.sorcinelli Owned by: boonebgorges
Milestone: 4.9 Priority: normal
Severity: normal Version: 4.6
Component: Taxonomy Keywords: has-patch has-unit-tests needs-testing
Focuses: Cc:

Description

The following suggested patch adds a new wp_get_object_terms_args filter in wp_get_object_terms() function in order to filter the arguments in retrieving the terms associated to the post. For example:

add_filter( 'wp_get_object_terms_args', 'wp_get_object_terms_args_hook', 10, 3 );
function wp_get_object_terms_args_hook ( $args, $object_ids, $taxonomies ) {
        $args['orderby'] = 'term_order';
        return $args;
}

wp_get_object_terms() is widely used to retrieve terms taxonomies but it's not possibile to set the term_order as orderby clause everywhere in a simple manner:

  • The posts list

Even if in the post list (wp-includes/category-template.php) you can uses get_the_terms filter to get ordered terms, you have to write additional code

  • The metabox taxonomy (advanced edit)

It seems that there is no filter/hook that allow to set the term_order as orderby for terms to edit (the terms_to_edit filter in wp-admin/includes/taxonomy.php seems to come too late :-( )

  • The XML-RCP interface

The patch filter will be used in wp_getPost() XML-RPC interface method by returning ordered terms

Comments and suggestion are appreciated

Kindest regards

PS1: Alternatively, with the get_object_terms filter you can do the orderby (among all other arguments) at the price to loose all the logic inside wp_get_object_terms() so you have to rebuild the terms array from scratch.

PS2: The attached patch can be applied to 4.4.2 version too.

Attachments (8)

035925.patch (875 bytes) - added by enrico.sorcinelli 5 years ago.
taxonomy.php-4.4.2.patch (817 bytes) - added by enrico.sorcinelli 5 years ago.
To apply patch: cd /path/to/wordpress-4.4.2/wp-includes; patch -p0 < taxonomy.php-4.4.2.patch
35925.2.patch (2.1 KB) - added by enrico.sorcinelli 4 years ago.
35925.patch (2.2 KB) - added by enrico.sorcinelli 4 years ago.
Updated to trunk. This patch replaces the previous ones
35925.3.patch (2.1 KB) - added by enrico.sorcinelli 3 years ago.
The updated the patch to the current trunk.
35925.4.patch (2.1 KB) - added by enrico.sorcinelli 3 years ago.
Updated to the current trunk.
35925.5.patch (2.1 KB) - added by enrico.sorcinelli 3 years ago.
Updated to the current trunk.
35925.6.patch (2.2 KB) - added by enrico.sorcinelli 3 years ago.
Uptated patch to current trunk and moved filter down.

Download all attachments as: .zip

Change History (24)

@enrico.sorcinelli
5 years ago

To apply patch: cd /path/to/wordpress-4.4.2/wp-includes; patch -p0 < taxonomy.php-4.4.2.patch

#1 @enrico.sorcinelli
5 years ago

  • Keywords has-patch added

#2 @chriscct7
4 years ago

  • Version trunk deleted

#3 @enrico.sorcinelli
4 years ago

  • Version set to trunk

I just updated the patch to trunk and I added test unit

#4 @DrewAPicture
4 years ago

  • Keywords has-unit-tests added

#5 @swissspidy
4 years ago

  • Milestone changed from Awaiting Review to Future Release

@enrico.sorcinelli
4 years ago

Updated to trunk. This patch replaces the previous ones

#6 @enrico.sorcinelli
4 years ago

Hi,

I just sent the updated path for current trunk

Regards

#7 @Chouby
4 years ago

Since WP 4.7, there is a workaround. Use the filter get_terms_args and test $args['object_ids'] to know if get_terms() has been called directly or through wp_get_object_terms(). Of course, this is a workaround and a dedicated filter as proposed here would offer a more robust solution.

#8 @SergeyBiryukov
4 years ago

  • Milestone changed from Future Release to 4.8

#9 @jbpaul17
3 years ago

  • Keywords needs-testing added

This needs review and an owner if it's going to land in 4.8.

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


3 years ago

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


3 years ago

#12 @jbpaul17
3 years ago

  • Milestone changed from 4.8 to 4.8.1

Punting to 4.8.1 per today's bug scrub.

@enrico.sorcinelli
3 years ago

The updated the patch to the current trunk.

#13 @enrico.sorcinelli
3 years ago

I just updated the patch to the current trunk.

@enrico.sorcinelli
3 years ago

Updated to the current trunk.

@enrico.sorcinelli
3 years ago

Updated to the current trunk.

#14 @boonebgorges
3 years ago

  • Milestone changed from 4.8.1 to 4.9

@enrico.sorcinelli Thank you for updating this patch for 4.9.

What do you think about moving the filter after the call to wp_parse_args()? This will simplify filter callbacks, since they'll always be able to expect arrays.

@enrico.sorcinelli
3 years ago

Uptated patch to current trunk and moved filter down.

#15 @enrico.sorcinelli
3 years ago

@boonebgorges Sure! It looks like a more clever thing.

I just updated the patch to current trunk and moved the filter down.

#16 @boonebgorges
3 years ago

  • Owner set to boonebgorges
  • Resolution set to fixed
  • Status changed from new to closed

In 40994:

Introduce wp_get_object_terms_args filter.

This filter allows developers to modify the arguments passed to
wp_get_object_terms() before the query is run.

Props enrico.sorcinelli.
Fixes #35925.

Note: See TracTickets for help on using tickets.