Skip Navigation

[Resolved] Add Access Custom Post Group to post on CRED submit

This thread is resolved. Here is a description of the problem and solution.

Problem:
How can I assign a certain Post Group created by Access to a Post, submitted by CRED?

Solution:
You might use custom code:
https://toolset.com/forums/topic/how-to-use-cred-post-form-to-set-post-group-for-new-cpt-post/#post-572829

This support ticket is created 7 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Author
Posts
#572777

I am trying to restrict front end view access to specific posts. Fx when an author of group-A creates a new CPT post, the Post Group should be set to group-A so that only members of that group can view the post.

Right now, I can of course set the Post Group for individual posts in the back end, but this is not feasible.

#572829

This is possible with soe Custom Code.

I suggest to use a CRED Post form, so you can use our API to hook your custom Code at the right moment.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

Or, you can hook into save_post() if you need this to happen on the backend.

The trick is to know the Access Post Group unique identifier.
You can get this from the Database by following this steps:
- Create your Access Post Group.
- Assign at least one post to it.
- Find the meta of this post in the postmeta table in the database
- Find the metakey _wpcf_access_group and copy it's metavalue.
It should look similar to wpcf-custom-group-a269970820bacb6c0384851664dbac44

After, use the below code sample to create your Custom Code which you will put in your Theme's Functions file.

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');
        }
    }
}

Please let me know if this helps!

#572837

Thanks, Beda.
This sounds doable but also a little scary 😉

Now that you know the basic use case, are you able to point me in another direction? Maybe outside Toolset with a plugin that maybe better or easier can support what I am trying to achieve?

I have been looking at the plugin User Access Manager by @gm_alex, however it does pretty much the same as Access Post Groups does.

#572983

Thanks, Beda.
This sounds doable but also a little scary ????

Now that you know the basic use case, are you able to point me in another direction? Maybe outside Toolset with a plugin that maybe better or easier can support what I am trying to achieve?

I have been looking at the plugin User Access Manager by @gm_alex, however it does pretty much the same as Access Post Groups does.

#573548

I do not know of any other Plugin that controls the access such granulated as Toolset Access and can interact with a Front End API.

I filed a request to make the Group Name for those Post Groups more logical or even print it in the settings page of Access so to use it in code.

The code I provided will work, what you need is the name of that Group.
If you query by the exact data I provided you, you can easily find it in the Database.

#573696

Thanks Beda.