associating a taxonomy
-
I’m trying to associate another taxonomy that’s already populated to the
wpm-testimonialcustom type, but new to this and can’t make it work yet. In theory, the simple hook below should be enough but it isn’t. Do you happen to know why?add_action( 'init', 'ew_associate_taxonomy_to_testimonials' ); function ew_associate_taxonomy_to_testimonials() { register_taxonomy_for_object_type( 'attachment_category', 'wpm-testimonial' ); }Thanks.
-
register_taxonomy_for_object_typeis for already registered taxonomies. You must useregister_taxonomyfirst.
https://developer.ww.wp.xz.cn/reference/functions/register_taxonomy/Yes,
attachment_categoryis already registered, by Media Library Assistant. I’ve over there already defined the exact tree of terms I want ST to have, too.Sorry, I misunderstood.
Normally, that would work. But MLA registers their taxonomy at a later priority, 8388607 to be exact, probably to be compatible with other media plugins.
Try this:
add_action( 'init', 'ew_associate_taxonomy_to_testimonials', PHP_INT_MAX );It works now 🙂
But now there’s the next bump ahead: ST Categories’s empty. How to make ST views to filter by such associated taxonomy?
Like custom fields, the view editor only knows about things registered by the plugin.
Like the meta query before, you will need to add a tax query.
https://codex.ww.wp.xz.cn/Class_Reference/WP_Query#Taxonomy_ParametersYes, thanks. Took me some time to figure out the logic and integration with ST, given the circumstances.
a) First, the full code:
/*********************************************************************/ /** * Strong Testimonials plugin: associate [attachment_category] to [wpm-testimonial] post-type * https://wpshout.com/add-existing-taxonomies-wordpress-custom-post-types/ */ add_action( 'init', 'ew_associate_taxonomy_to_testimonials', PHP_INT_MAX ); function ew_associate_taxonomy_to_testimonials() { register_taxonomy_for_object_type( 'attachment_category', 'wpm-testimonial' ); } /** * Strong Testimonials plugin: associated taxonomy filter * https://ww.wp.xz.cn/support/topic/associating-a-taxonomy/#post-9499788 */ add_filter( 'wpmtst_query_args', 'ew_wpmtst_query_args__filter', 10, 2 ); function ew_wpmtst_query_args__filter( $args, $atts ) { // filtering testimonials by associated taxonomy terms // populated associated term(s) get compared against dummy ST category term(s) to match the query if( isset( $args['tax_query'] ) && !empty( $atts['category'] ) ): // get ST category slug(s) for matching the term(s) in the associated taxonomy $cat_ids = explode( ",", $atts['category'] ); foreach( $cat_ids as $cat_id ) { // ensure only valid slugs get retrieved (pattern) if( empty( $cat_slugs[] = get_term_by( 'id', $cat_id, 'wpm-testimonial-category' )->slug ) ) array_pop( $cat_slugs ); } // replace query for associated taxonomy instead of native $args['tax_query'] = array( array( 'taxonomy' => 'attachment_category', 'field' => 'slug', 'terms' => $cat_slugs, 'operator' => 'IN', 'include_children' => true ) ); endif; // error_log( var_export( $args, true ) ); return $args; }b) Further Explanation:
ST Views should work as usual, but checking ‘Categories’ now has a different meaning. They’re just labels for matching the associated taxonomy terms. Example:
Category slug: 'woodblocks' (0 items) Assoc. Category slug: 'woodblocks' (10 items) wp_query returns: 10 itemsOf course, each testimonial must have checked some terms in the associated taxonomy tree first, which now appears as an extra box in the Testimonials Editor. Hitting any ST Categories isn’t required, but the label terms for a match must be present when setting a View.
If for some reason one would still prefer to use both taxonomies, then need to designate a view for the association and add a check for it, like this:
if( ( $atts['view'] == '1' ) /* [testimonial_view id=1] */ && isset( $args['tax_query'] ) && !empty( $atts['category'] ) ):HTH.
What I still miss is displaying the associated taxonomy column in the ‘All Testimonials’ page. I’m almost certain it should be a std. WP way of doing it. Could you hint how?
EDIT:
Nevermind, found it how. It was easy this time. 🙂
/** * Strong Testimonials plugin: add assoc. taxonomy column to the 'All Testimonials' page * using the 'manage_taxonomies_for_{$post_type}_columns' filter * https://make.ww.wp.xz.cn/core/2012/12/11/wordpress-3-5-admin-columns-for-custom-taxonomies/ */ add_filter( 'manage_taxonomies_for_wpm-testimonial_columns', 'ew_wpm_testimonial_type_columns' ); function ew_wpm_testimonial_type_columns( $taxonomies ) { $taxonomies[] = 'attachment_category'; return $taxonomies; }
The topic ‘associating a taxonomy’ is closed to new replies.