Make WordPress Core

Opened 14 years ago

Closed 10 years ago

#19094 closed enhancement (wontfix)

Add wp_get_object_terms filters.

Reported by: zatsugami's profile Zatsugami Owned by:
Milestone: 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 14 years ago.
Taxonomy patch. New filters for wp_get_object_terms giving access to select query.

Download all attachments as: .zip

Change History (9)

#1 @scribu
14 years ago

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

#2 follow-up: @Zatsugami
14 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 14 years ago by Zatsugami (previous) (diff)

#3 in reply to: ↑ 2 @SergeyBiryukov
14 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

@Zatsugami
14 years ago

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

#4 @Zatsugami
14 years ago

  • Keywords has-patch added

#5 @scribu
14 years ago

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

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

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

#6 @Zatsugami
14 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.

#7 @badconker
13 years ago

  • Cc aurelien.joahny@… added

#8 @boonebgorges
10 years ago

  • Milestone Future Release deleted
  • Resolution set to wontfix
  • Status changed from new to closed

Thanks for the ticket, Zatsugami, and sorry for the delay in response.

Filtering SQL clauses is something we're loath to do except when absolutely necessary, as it makes it very difficult for us to make future changes to the queries involved without breaking backward compatibility. As you note, in your case it's possible to use the 'wp_get_object_terms' array to do what you need, and I'd guess that the same is true for the majority of potential use cases, which suggests that the SQL filter is unnecessary.

I'm glad to be overruled in the case of overwhelming evidence to the contrary, but for the time being, I'm going to mark this one wontfix.

Note: See TracTickets for help on using tickets.