Inherit parent taxonomy when creating child post
-
Im doing a register form that creates a new post when submitted, the new post is child of another programatically defined post so i want the new creted post to inherit the categories from its parent, i have this function working perfectly to do this when i create the post from the wordpress ui:
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 );
But when i create the post using this code:
$horario = array( 'post_title' => $user_first." ".$user_last, 'post_content' => 'Nota:'.$horario_dj, 'post_status' => 'publish', 'post_author' => $new_user_id, 'post_type' => 'horarios', 'post_parent' => $horario_dj, 'tax_input' => $custom_tax ); $new_post_ID = wp_insert_post($horario);
the function is not getting called, i already tried changing the “save_post” hook to everyhing there is related to creating a post, i even tried calling the function directly from the submit validation after the post is created with this code:
$post_object = get_post($new_post_ID); set_parent_terms( $new_post_ID, $post_object )
but nothing seems to work, have any ideas?
- The topic ‘Inherit parent taxonomy when creating child post’ is closed to new replies.