skip to Main Content

I want such a thing that searched over and over but nothing found!
I have about 15 authors and imported +1000 posts from my previous website. And all of them are submitted for one and same author. But I need to assign posts to random authors.
Is there any functions, plugin , etc to doing this?

2

Answers


  1. You can do something like

    $posts = get_posts([
      'posts_per_page' => -1 //retrieve all posts
    ]);
    
    $users_ids = [1,2,3]; //replace with the desired user id's
    
    foreach($posts as $post){
    
      $arg = [
        'ID' => $post->ID,
        'post_author' => array_rand($users_ids), //random user ID
      ];
    
      wp_update_post($arg); 
    
    }
    
    

    In the example above I’m pulling all the posts (of a post type post), if you need to do it only on specific posts, you’ll have to change the get_posts() arguments to get specific post id’s or range of post id’s

    Login or Signup to reply.
    1. Get all the necessary users
    2. Get the posts you need
    3. Update authors to posts
    $users = get_users( ['fields' => 'ID'] );
    
    $posts = get_posts( [
        'post_type'      => 'your_custom_post_type',
        'posts_per_page' => -1,
        'fields'         => 'ids',
    ] );
    
    foreach ( $posts as $post_id ) {
        wp_update_post( [
            'ID'          => $post_id,
            'post_author' => array_rand( $users ),
        ] );
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search