$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>front end - How to update the children of a post?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

front end - How to update the children of a post?

matteradmin6PV0评论

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.

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.

Share Improve this question asked Nov 19, 2018 at 4:16 PlatoPlato 31 bronze badge 2
  • What is the type of these posts? – Krzysiek Dróżdż Commented Nov 19, 2018 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 Commented Nov 19, 2018 at 15:07
Add a comment  | 

1 Answer 1

Reset to default 0

get_children returns an array of post objects by default:

https://developer.wordpress/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
                )
            );
        }
    }
}
Post a comment

comment list (0)

  1. No comments so far