Skip to content

Instantly share code, notes, and snippets.

@mrfoxtalbot
Created April 30, 2016 23:14
Show Gist options
  • Save mrfoxtalbot/018137eeee59d9402431217d5eb65e40 to your computer and use it in GitHub Desktop.
Save mrfoxtalbot/018137eeee59d9402431217d5eb65e40 to your computer and use it in GitHub Desktop.
Hacer que los posts hijos hereden los términos de los posts padres
/** Set Child Terms to Parent Terms **/
function set_parent_terms( $post_id, $post ) {
if ( 'publish' === $post->post_status && $post->post_parent > 0 ) {
$parent = get_post($post->post_parent);
if(!empty($parent)){
$taxonomies = get_object_taxonomies( $parent->post_type );
foreach ( (array) $taxonomies as $taxonomy ) {
$terms = wp_get_post_terms( $parent->ID, $taxonomy );
if ( !empty( $terms ) ) {
$termArr = array_map(create_function('$obj', 'return $obj->term_id;'), $terms);
$tmp = wp_set_object_terms( $post_id, $termArr, $taxonomy, true );
}
}
}
}
}
add_action( 'save_post', 'set_parent_terms', 100, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment