WordPress.org

Make WordPress Core

Opened 2 years ago

Last modified 23 months ago

#19094 new enhancement

Add wp_get_object_terms filters.

Reported by: Zatsugami Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.2.1
Component: Taxonomy Keywords: has-patch 2nd-opinion
Focuses: Cc:

Description

I wasted whole day looking for some filter I could use for changing the select query of wp_get_object_terms.

Please add filter like terms_clauses but for wp_get_object_terms (or use the same filter), because terms_clauses is NOT called when there are terms selected for the object. For example. When you select categories for post.

Attachments (1)

taxonomy.patch (1.8 KB) - added by Zatsugami 2 years ago.
Taxonomy patch. New filters for wp_get_object_terms giving access to select query.

Download all attachments as: .zip

Change History (8)

comment:1 scribu2 years ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to Future Release
  • Type changed from feature request to enhancement

comment:2 follow-up: Zatsugami2 years ago

Ok, I've done it myself. It should work.
Basically copy-paste of posts_clauses.
It's somewhere around line 1897 wp-inlcudes/taxonomy.php

Hmm, it may be a stupid question, but can I apply this to be a patch?
If so, how?

$pieces = array( 'select', 'join', 'where', 'orderby', 'order' );

// Setting up initial $query pieces, some are already present
$select = $select_this;
$join 	= "INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id";
$where 	= "WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids)";

// We applay filters
$select 	= apply_filters( 'object_terms_select', $select );
$join 		= apply_filters( 'object_terms_join', $join );
$where 		= apply_filters( 'object_terms_where', $where );
$orderby 	= apply_filters( 'object_terms_orderby', $orderby);
$order 		= apply_filters( 'object_terms_order', $order);

// Filter all clauses at once, for convenience
$clauses = (array) apply_filters( 'object_terms_clauses', compact( $pieces ) );
foreach ( $pieces as $piece )
	$$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
	
$query = "SELECT $select FROM $wpdb->terms AS t $join $where $orderby $order";

$query = apply_filters('object_terms_query', $query);
Last edited 2 years ago by Zatsugami (previous) (diff)

comment:3 in reply to: ↑ 2 SergeyBiryukov2 years ago

Replying to Zatsugami:

Hmm, it may be a stupid question, but can I apply this to be a patch?
If so, how?

http://codex.wordpress.org/Reporting_Bugs#Patching_Bugs

Zatsugami2 years ago

Taxonomy patch. New filters for wp_get_object_terms giving access to select query.

comment:4 Zatsugami2 years ago

  • Keywords has-patch added

comment:5 scribu2 years ago

  • Keywords 2nd-opinion added; needs-patch removed

Ok, so are you sure this filter can't be to a higher-level function?

Version 0, edited 2 years ago by scribu (next)

comment:6 Zatsugami2 years ago

In my case I needed to join mysql table, so probably there is no other way to do it.
There is posts_clauses, terms_clauses (as I mentioned, it is not called when getting objects terms), but there is no object_terms_clauses or any other filters.

I could use filter 'wp_get_object_terms' which is called at the end of this function, but what you get there is an array of terms, so I would need to loop through them, get the ids of terms and do other query. Anyway, so much unnecessary work would be done.

Also, there would be a small inconsistency in this patch in regards to pieces names in object_terms_clauses array. The 'select' piece in other filters is called 'fields';
But there would be more work to do with the code to make the names the same. But this can be done if you prefer consistency.

comment:7 badconker23 months ago

  • Cc aurelien.joahny@… added
Note: See TracTickets for help on using tickets.