Opened 19 months ago
Last modified 12 months ago
#19094 new enhancement
Add wp_get_object_terms filters.
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Future Release |
| Component: | Taxonomy | Version: | 3.2.1 |
| Severity: | normal | Keywords: | has-patch 2nd-opinion |
| Cc: | aurelien.joahny@… |
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)
Change History (8)
- Keywords needs-patch added
- Milestone changed from Awaiting Review to Future Release
- Type changed from feature request to enhancement
comment:3
in reply to:
↑ 2
SergeyBiryukov — 19 months ago
Replying to Zatsugami:
Hmm, it may be a stupid question, but can I apply this to be a patch?
If so, how?
Taxonomy patch. New filters for wp_get_object_terms giving access to select query.
- Keywords 2nd-opinion added; needs-patch removed
Ok, so are you sure this filter can't be added to a higher-level function?
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.

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);