How to set child post categories to parent post categories when updating parent post?

I’m hoping someone can help me. I’ve googled this so much, that Google has pegged me as a bot, and I’ve had to fill out like ten captchas to verify I’m human.

So far, I have the following, which only updates the child post categories to match the parent post categories on update of the child post, but I need it to happen when updating the parent post, otherwise I have to manually update each child post whenever I make a change to the parent post.

/** Set Child Terms to Parent Terms on Update **/
function set_parent_terms_update( $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) $categories as $category ) {
$terms = wp_get_post_terms( $parent->ID, $category );
if ( !empty( $terms ) ) {
$termArr = array_map(create_function(‘$obj’, ‘return $obj->term_id;’), $terms);
$tmp = wp_set_object_terms( $post_id, $termArr, $category, true );
}
}
}
}

Read more here:: How to set child post categories to parent post categories when updating parent post?

Leave a Reply

Your email address will not be published. Required fields are marked *