Title: Set roles to post programmatically
Last modified: September 2, 2019

---

# Set roles to post programmatically

 *  Resolved [Piccolanomade](https://wordpress.org/support/users/piccolanomade/)
 * (@piccolanomade)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/set-roles-to-post-programmatically/)
 * Hi,
    I did the migration for more than 500 posts from my old site to my new own.
 * I installed Members plugin before that. Now, these posts are without roles.
    
   I’d like to assign some specific roles programmatically, I prefer don’t modify
   every single post 🙂
 * I tried this loop:
 *     ```
       <?php
       require_once('wp-load.php');
   
       if ( !is_user_logged_in() && !current_user_can('manage_options')) {
       	die('Please login as admin to use this page.');
       }
   
       $roles = array('administrator','author','support');
   
       // the query
       $wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1)); 
   
       if ( $wpb_all_query->have_posts() ) : 
       ?>
       <ul>
           <?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); 
       		members_set_post_roles( the_ID(), array_map( 'members_sanitize_role', $roles ) );		
       	?>
           <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
           <?php endwhile; ?>
       </ul>
       	<?php wp_reset_postdata(); 
   
        else : ?>
           <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
       <?php endif; ?>
       ```
   
 * I put this file.php into my installation root. It’s print all post Title and 
   I have this warning, but nothing happened to posts’ roles.
    Warning: in_array()
   expects parameter 2 to be array, boolean given in /home/wpwebsit/public_html/
   PMSknow/wp-content/plugins/members/inc/functions-content-permissions.php on line
   105
 * Can someone help me to do this?
    THANKS
 * Regards

Viewing 1 replies (of 1 total)

 *  [Justin Tadlock](https://wordpress.org/support/users/greenshady/)
 * (@greenshady)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/set-roles-to-post-programmatically/#post-11900016)
 * `get_the_ID()` instead of `the_ID()` is something I immediately noticed that 
   is incorrect. Maybe that’ll help.

Viewing 1 replies (of 1 total)

The topic ‘Set roles to post programmatically’ is closed to new replies.

 * ![](https://ps.w.org/members/assets/icon-256x256.png?rev=3508404)
 * [Members - Membership & User Role Editor Plugin](https://wordpress.org/plugins/members/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/members/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/members/)
 * [Active Topics](https://wordpress.org/support/plugin/members/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/members/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/members/reviews/)

## Tags

 * [Coding](https://wordpress.org/support/topic-tag/coding/)
 * [posts](https://wordpress.org/support/topic-tag/posts/)

 * 1 reply
 * 2 participants
 * Last reply from: [Justin Tadlock](https://wordpress.org/support/users/greenshady/)
 * Last activity: [6 years, 9 months ago](https://wordpress.org/support/topic/set-roles-to-post-programmatically/#post-11900016)
 * Status: resolved