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.
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!
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.
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.
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.