Detect Terms of Current Post on Save
-
I’m attempting to automatically assign terms from a parent post to automatically generated child pages. I am able to detect all children for the given post and can set a term however when attempting to collect the post terms of the parent post, wordpress returns an empty array. All post terms are set in the parent post on initial set up but are not passed into the variable i’ve created.
see below for sample function added to my functions.php.
<?php add_action('save_post', 'assign_parent_terms',10); function assign_parent_terms($post_id){ $currentpost = get_post( $post_id );; if(isset($post) && $post->post_type != 'campaign'){ return $post_id;} // get all assigned terms // check nonces and capabilities here. // use a ternary statement to make sure the input is actually set // remember that <code>save_post</code> fires on every post save (any post type). // Returns Array of Term ID's for "my_taxonomy". $term_list = wp_get_object_terms( $currentpost->ID, 'brand'); //$term_list = wp_get_post_terms( $post->ID, 'brand'); $getchilargs = array( 'post_parent' => $post->ID, 'post_type' => 'campaign', 'numberposts' => -1, 'post_status' => 'any' ); $children = get_children( $getchilargs ); if(!empty($children)){ foreach ($children as $child){ $childID = $child->ID; //$childparent = $child->parent; //$term_list = $post->post_name; wp_set_object_terms($childID, $term_list, 'brand'); } } } ?>
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Detect Terms of Current Post on Save’ is closed to new replies.