Created
November 25, 2019 19:09
-
-
Save tripflex/33025718246b4ffb0050058dd8a69fe3 to your computer and use it in GitHub Desktop.
Automatically set/assign child taxonomy terms for hierarchical taxonomies in WordPress (job listings, with job_listing_category taxonomy)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'set_object_terms', 'auto_set_child_terms', 9999, 6 ); | |
/** | |
* Automatically set/assign child taxonomy terms to posts | |
* | |
* This function will automatically set child taxonomy terms whenever a parent term is set on a post, | |
* with the option to configure specific post types, and/or taxonomies. | |
* | |
* | |
* @param int $object_id Object ID. | |
* @param array $terms An array of object terms. | |
* @param array $tt_ids An array of term taxonomy IDs. | |
* @param string $taxonomy Taxonomy slug. | |
* @param bool $append Whether to append new terms to the old terms. | |
* @param array $old_tt_ids Old array of term taxonomy IDs. | |
*/ | |
function auto_set_child_terms( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) { | |
/** | |
* We only want to move forward if there are taxonomies to set | |
*/ | |
if ( empty( $tt_ids ) ) { | |
return false; | |
} | |
/** | |
* Set specific post types to only set parents on. Set $post_types = FALSE to set parents for ALL post types. | |
*/ | |
$post_types = array( 'job_listing' ); | |
if ( $post_types !== false && ! in_array( get_post_type( $object_id ), $post_types ) ) { | |
return false; | |
} | |
/** | |
* Set specific post types to only set parents on. Set $post_types = FALSE to set parents for ALL post types. | |
*/ | |
$tax_types = array( 'job_listing_category' ); | |
if ( $tax_types !== false && ! in_array( $taxonomy, $tax_types ) ) { | |
return false; | |
} | |
foreach ( $tt_ids as $tt_id ) { | |
$children = get_term_children( $tt_id, $taxonomy ); | |
if ( ! empty( $children ) ) { | |
wp_set_post_terms( $object_id, $children, $taxonomy, true ); | |
} | |
} | |
} |
Hey Myles - wondering if this gist could work in the reverse? I can't think of a particular use case for your way around but in my case I want Parent taxonomies to be automatically set when any of its children are selected, for example; if someone selects "London" or "Manchester" then the parent "United Kingdom" would automatically be set?
@JasonBroderick Yup right here:
https://gist.github.com/tripflex/65dbffc4342cf7077e49d641462b46ad
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Myles - wondering if this gist could work in the reverse? I can't think of a particular use case for your way around but in my case I want Parent taxonomies to be automatically set when any of its children are selected, for example; if someone selects "London" or "Manchester" then the parent "United Kingdom" would automatically be set?