1

*****EDIT*** If I can figure out how to auto-update the child posts whenever the parent post is updated, the first function below will work.**

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 Publish **/
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 );

I thought maybe something like this could work, but I honestly have no idea what to do to get it functional. I'm sure I'm doing more than a few things wrong here.

//Hopefully update the child cats when parent post is updated
function update_child_cats( $post_id, $post ){

if ( 'publish' === $post->post_status && $post->post_parent = 0 ){


function wp_get_post_categories( $post_id = 0, $args = array() ) {
$post_id = (int) $post_id;

$defaults = array('fields' => 'ids');
$args = wp_parse_args( $args, $defaults );

$cats = wp_get_object_terms($post_id, 'category', $args);
return $cats;
}


$args = array(
    'post_parent' => 0,
    'fields' => 'ids',
);
$ChildIDs = get_children( $args );

$ParentCategoryIDs = $cats;

// Update post 37
  $my_post = array(
      'ID'    => array($ChildIDs), //$ChildIDs returned from children above
      'cat'   => array($ParentCategoryIDs), //returned from above.
  );

// Update the post into the database
  wp_update_post( $my_post );

  }
}
add_action( 'post_updated', 'update_child_cats', 100, 2 );
  • What is your specific question? – WebElaine Mar 12 '18 at 14:58
  • Hi WebElaine, thank you for your response. I'm hoping you can help me. I'm trying to figure out how to automatically update "child post" categories to match the "parent post" categories whenever the parent post categories are updated. It seems pretty basic, but I can't seem to find any examples on how to do this. I updated the question with some code I patched together to attempt to illustrate what I'm trying to do. Please let me know if you need any more clarification. – alice Mar 12 '18 at 15:10
  • In other words, any child posts that have a parent post should automatically be updated to match the categories of the parent post whenever the parent post is updated. Does that make sense? – alice Mar 12 '18 at 15:12
  • Ok. One thing to change in your code is your if statement - you currently have $post->post_parent = 0 which sets the post parent rather than checking. Replace the single equals with double equals, i.e. $post->post_parent == 0. – WebElaine Mar 12 '18 at 15:53
  • Thanks, that definitely changed something, when I tried to hit update on the parent post, it kept spinning for a few minutes... I refreshed the page and it took the new categories, but unfortunately, the child post did not take the new categories selected on parent post. – alice Mar 12 '18 at 16:01
0

So I was able to temporarily achieve the functionality I needed by pairing the set_parent_terms function in the top section of code, which fires only when the child posts are saved, with a completely different function I managed to scrape together, that merely auto-saves the child posts when the parent post is saved. See completely separate solution here, which triggers the update of the children required by the first function above.

Still looking for an answer to the above to wrap it all into one function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.