Updating post_parent by id (many times)

How can I update post_parent by ids?

I tried:

add_action('init','updating_parent');

function updating_parent(){

    $args = array(
       'post__in' = array(208,51) 
    );

    $my_posts = get_posts( $args );

    foreach ( $my_posts as $my_post ):

        $my_post['post_parent'] = 12;

        wp_update_post( $my_post );

    endforeach;
}

But this is not working. But even it wouldn't be what I need. Since I have data like:

ID = post_parent
208 = 12
51 = 19
and so on...

So I need an array at post_parent as well.

Topic wp-update-post Wordpress

Category Web


A post can only ever have one parent post.

I'm certain you can still accomplish what you want using post meta, though. If you give us a better explanation of what you're trying to do we might be able to help.

For example,

function updating_parent() {
    // your code to get posts

    foreach ( $my_posts as $post ) {
        update_post_meta( $post->ID, 'related_posts', array( 12, 23, 42 ) );
    }
}

Then you can access this data using get_post_meta:

$related_posts = get_post_meta( get_the_ID(), 'related_posts', true );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.