Detect Terms of Current Post on Save
Question
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 save_post 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’);
}
}
}
?>
0
custom-post-types, custom-taxonomy, save-post, terms
3 years
2020-04-06T12:53:38-05:00
2020-04-06T12:53:38-05:00 0 Answers
91 views
0
Leave an answer