0

To clarify, this question is merely on how to "save" or "update" a child post when the parent post is saved. Nothing more.

Hello to all of you brilliant and wonderful people. I need to auto-update all the child posts whenever the parent post is saved because I have a function that updates the categories to match the parent whenever the child post is updated. This should be pretty straightforward. The only catch is the child posts are a different post type than the parent, but they both share the same category taxonomy (child posts do not have unique categories). I'm thinking it can be done through 'save_post' function. Thanks in advance!!! Much much appreciated!!!

  • Please update your original question rather than adding a duplicate. The fact that these are different post types may be an important key - how are they "child" posts if they're not the same cpt? – WebElaine Mar 12 '18 at 19:42
  • This is not a duplicate. It's a different question entirely. I'm looking for an entirely different function that is merely a complement to my other question, and I separated the two because they are entirely different functionalities and future viewers may have questions about one or the other. How are they child posts if parent post is different cpt? They have the parent ID of the different post type. – alice Mar 12 '18 at 19:48
  • Figured out a solution to this completely different question below, if you're interested. Thanks for the downvote, btw. – alice Mar 14 '18 at 11:01
1

Hello again beautiful WP community. I managed to scrape together a solution in case anyone needs it. I'm sure it needs some cleaning up, but it works. The awesome thing is that it's multi-purpose — just change the $args to hardcode any change to a tag, key, category, title, author, status, template or content in the child post whenever the parent post is updated.

What it doesn't do: Unfortunately, one overzealous user who was unable to help erroneously downvoted this question as a duplicate (it's not). This completely different function does not auto-update the child taxonomies to match the parent's taxonomies when the parent post is saved (still looking for an answer to that), but in the meantime, you can pair the following function with the top section of code in this completely different question here in order to achieve that.

Keep making this place awesome!

add_action( 'save_post', 'update_children', 10, 2);
function update_children( $post_id, $post ) {

if ( 'trash' != $post->post_status ){

    // Is a top level post
    if( 0 === $post->post_parent ) {
        $children = array(
            'post_parent'   => $post->ID,
            'post_type'         => 'any',   //you can use also 'any'
            'fields'            => 'ids',   // Only Return IDs
            'post_status' => 'publish',
        );
        global $post;

        if ((is_array($children))&&(!empty( $children ))){
            $the_query = new WP_Query( $children );
        }
        // The Loop
        if ( $the_query->have_posts() ) :
        while ( $the_query->have_posts() ) : $the_query->the_post();
        $mypostids[] = get_the_ID();
        $updatethis = array (
                'fields'      => $mypostids,
                'post_status' => 'publish'
        );

        if ((is_array($children))&&(!empty( $children ))) {     
            foreach( $children as $child_ids ) {
                remove_action( 'save_post', 'update_children', 10, 2);
                wp_update_post( $updatethis );
                add_action( 'save_post', 'update_children', 10, 2);
            }           
        }
        endwhile;
        endif;      
    }
}
}

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.