Support » Plugin: Members » Set roles to post programmatically

  • Piccolanomade

    (@piccolanomade)


    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)
  • Plugin Author Justin Tadlock

    (@greenshady)

    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)
  • You must be logged in to reply to this topic.