0

i found nothing about it in the WP reference and Google, so i ask you: I want to update a post by a form - and with it its children.

      $post = array(

            'ID'             => $mainid,  
            'post_status'   =>   'wartend'
        );
        $lead = wp_update_post($post);



$children = get_children( $mainid );

foreach ($children as $child){
    wp_update_post(
    array(
        'ID' => $child, 
        'post_status'   =>   'wartend',
        'post_parent' => $mainid
    )
);
}

The post is getting updated, but not the children.

  • What is the type of these posts? – Krzysiek Dróżdż Nov 19 '18 at 6:22
  • Its a custom post type ^^ i didnt liked woocommerce, so i wrote a own shopping solution with custom fields and custom post type. Was more affort than i thought, but works really well. – Plato Nov 19 '18 at 15:07
0

get_children returns an array of post objects by default:

https://developer.wordpress.org/reference/functions/get_children/

So you would have to use 'ID' => $child->ID, in this case... also my want to wrap the foreach with if (count($children) > 0) {} to prevent possible errors where there are no children. ie:

$children = get_children( $mainid );

if (count($children) > 0) {
    foreach ($children as $child){
        wp_update_post(
            array(
                'ID' => $child->ID, 
                'post_status'   =>   'wartend',
                'post_parent' => $mainid
                )
            );
        }
    }
}
  • no probs @Plato, if this solved your problem feel free to mark answer as correct ;-) – majick Nov 19 '18 at 14:08

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.