How to set a Access group to a post which is submitted by CRED

The Post stores the Access Custom Group as a meta field (custom post field)

You can update your Post using WordPress and CRED API, by addressing the hidden Custom Field Key:

_wpcf_access_group
The Value is the name you gave to the group, prefixed with

wpcf-custom-group-

So you can use a cred_save_data action, with update_post_meta

Apply this code on the CRED you are using and every new posts will have the proper (coded by you) Access group stored.

add_action('cred_save_data', 'my_save_data_action',10,2);
 
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==91)//Change to your CRED Form ID you use
    {
        $user = wp_get_current_user(); //We get the current user data
         
         if ( in_array( 'administrator', (array) $user->roles ) ) { //My example checks for admins. You can use any Custom Role as well here
           update_post_meta($post_id,'_wpcf_access_group','wpcf-custom-group-1584797355f2d242ba5c8c879e748829');//We update the metafield for the post created, and pass the correct Access Group ID. This is the value you need to find in the database.
        }
        else {//something else if above is not true
            update_post_meta($post_id,'_wpcf_access_group','wpcf-custom-group-a269970820bacb6c0384851664dbac44');
        }
    }
}

 

Relevant Documentation:
https://codex.wordpress.org/Function_Reference/update_post_meta

https://wp-types.com/documentation/user-guides/cred-api/#csd

https://wp-types.com/forums/topic/how-to-use-cred-post-form-to-set-post-group-for-new-cpt-post/#post-572829


Let us know if this snippet is not working for you:

This snippet doesn’t work
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments