Title: mfwebmaster's Replies | WordPress.org

---

# mfwebmaster

  [  ](https://wordpress.org/support/users/mfwebmaster/)

 *   [Profile](https://wordpress.org/support/users/mfwebmaster/)
 *   [Topics Started](https://wordpress.org/support/users/mfwebmaster/topics/)
 *   [Replies Created](https://wordpress.org/support/users/mfwebmaster/replies/)
 *   [Reviews Written](https://wordpress.org/support/users/mfwebmaster/reviews/)
 *   [Topics Replied To](https://wordpress.org/support/users/mfwebmaster/replied-to/)
 *   [Engagements](https://wordpress.org/support/users/mfwebmaster/engagements/)
 *   [Favorites](https://wordpress.org/support/users/mfwebmaster/favorites/)

 Search replies:

## Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)

 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [[Groups] Using the groups API to assign a group to the CPT, post and page upon creation](https://wordpress.org/support/topic/using-the-groups-api-to-assign-a-group-to-the-cpt-post-and-page-upon-creation/)
 *  Thread Starter [mfwebmaster](https://wordpress.org/support/users/mfwebmaster/)
 * (@mfwebmaster)
 * [7 years, 8 months ago](https://wordpress.org/support/topic/using-the-groups-api-to-assign-a-group-to-the-cpt-post-and-page-upon-creation/#post-10611328)
 * Hi All
    got it working for CPT Page and Post not working
 * <?php
 * ////////////////////////////////////////////////////////////////////
    // Add 
   Hooks for Changing the Group of a page ////////////////////////////////////////////////////////////////////
   add_action(‘new_to_publish’, ‘set_post_to_group’); function set_post_to_group(
   $post){ //////////////////////////////////////////////////////////////////// //
   Set Variables ////////////////////////////////////////////////////////////////////
   $authorId = get_the_author_meta( ‘ID’ ); $group_id = do_shortcode( ‘[groups_user_groups
   user_id=”‘ . $authorId . ‘”]’ ); $group_id_strip = strip_tags($group_id); $post_id
   = $post->ID; $post_type = ‘CPT Name’; //post, page, attachment or whatever other
   CPT you may have //////////////////////////////////////////////////////////////////////
   Call function ////////////////////////////////////////////////////////////////////
   if(get_post_type($post_id) == $post_type) { if ( $group = Groups_Group::read_by_name(
   $group_id_strip ) ) { // Add user to group $post_type Groups_Post_Access::create(
   array( ‘post_id’ => $post_id, ‘group_id’ => $group -> group_id ) ); } } } ?>
 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [[Groups] Using the groups API to assign a group to the CPT, post and page upon creation](https://wordpress.org/support/topic/using-the-groups-api-to-assign-a-group-to-the-cpt-post-and-page-upon-creation/)
 *  Thread Starter [mfwebmaster](https://wordpress.org/support/users/mfwebmaster/)
 * (@mfwebmaster)
 * [7 years, 8 months ago](https://wordpress.org/support/topic/using-the-groups-api-to-assign-a-group-to-the-cpt-post-and-page-upon-creation/#post-10609242)
 * Hi a updated code but still not working.. Please can someone assist me.
 * <?php
 * ////////////////////////////////////////////////////////////////////
    // Add 
   Hooks for Changing the Group of a page ////////////////////////////////////////////////////////////////////
 * add_action(‘future_to_publish’, ‘set_post_to_group’);
    add_action(‘new_to_publish’,‘
   set_post_to_group’); add_action(‘draft_to_publish’ ,’set_post_to_group’); add_action(‘
   auto-draft_to_publish’ ,’set_post_to_group’);
 * function set_post_to_group($post){
    //////////////////////////////////////////////////////////////////////
   Set Variables ////////////////////////////////////////////////////////////////////
   $authorGroup = get_the_author_meta( ‘ID’ ); $group_id = do_shortcode( ‘[groups_user_groups
   user_id=”‘ . $authorGroup . ‘”]’ ); $storeGroup_strip = strip_tags($user_group);
   $post_id = $post->ID;
 *  ////////////////////////////////////////////////////////////////////
    // Call
   function ////////////////////////////////////////////////////////////////////
   if(get_post_type($post_id) === ‘page’) { //post, page, attachment or whatever
   other CPT you may have Groups_Post_Access::create( array( ‘post_id’ => $post_id,‘
   group_id’ => $storeGroup_strip, ) ); } }
 * ?>

Viewing 2 replies - 1 through 2 (of 2 total)