Thank you very much for your advice and help. Can I please ask you to assist with some syntax, because my PHP is very poor to non-existent.
I have created a post group 'Private' and assigned a post to test for the entry, which is wpcf-custom-group-2c17c6393771ee3048ae34d6b380c5ec
I have then created a field 'keep-private' which is a tick box, which will store the value '1' if it is ticked.
I would like to assign the post group to posts on save if this field is ticked. Based on your syntax is the update part correct? I'm not sure how to check the field value.
add_action( 'save_post', 'automate_group', 100, 3 );
function automate_group( $post_id, $post, $update ) {
// add your code here to test the post's terms or custom field values
// and apply the correct post group
update_post_meta($post_id,'_wpcf_access_group','wpcf-custom-group-2c17c6393771ee3048ae34d6b380c5ec9');
}
Since this is a save post hook then you need to check the value of the custom field by doing this.
add_action( 'save_post', 'automate_group', 100, 3 );
function automate_group( $post_id, $post, $update ) {
// add your code here to test the post's terms or custom field values
$field_val = get_post_meta($post_id, 'wpcf-my-field');
// and apply the correct post group
if ( $field_val == 'some-val'){
update_post_meta($post_id,'_wpcf_access_group','wpcf-custom-group-2c17c6393771ee3048ae34d6b380c5ec9');
}
}
Now all you should need to do is replace some-val with the value you want to test with and wpcf-my-field to the name of your custom field with the wpcf- prefix.
Please let me know the results of this.
Thanks,
Shane
Thanks, but it doesn't work. I am checking wpcf-keep-private for the tick (1) and updating the 'private' post group but it doesn't work - the post does not get added to the group. Have I got something wrong here?
// add your code here to test the post's terms or custom field values
$field_val = get_post_meta($post_id, 'wpcf-keep-private');
// and apply the correct post group
if ( $field_val == '1'){
update_post_meta($post_id,'_wpcf_access_group','wpcf-custom-group-2c17c6393771ee3048ae34d6b380c5ec');
}
}
add_action( 'save_post', 'automate_group', 100, 3 );
function automate_group( $post_id, $post, $update ) {
// add your code here to test the post's terms or custom field values
$field_val = get_post_meta($post_id, 'wpcf-keep-private');
// and apply the correct post group
if ( $field_val[0] == '1'){
update_post_meta($post_id,'_wpcf_access_group','wpcf-custom-group-2c17c6393771ee3048ae34d6b380c5ec');
}
}
To remove when the value is unchecked you can do this.
add_action( 'save_post', 'automate_group', 100, 3 );
function automate_group( $post_id, $post, $update ) {
// add your code here to test the post's terms or custom field values
$field_val = get_post_meta($post_id, 'wpcf-keep-private');
// and apply the correct post group
if ( $field_val[0] == '1'){
update_post_meta($post_id,'_wpcf_access_group','wpcf-custom-group-2c17c6393771ee3048ae34d6b380c5ec');
}
if ( empty($field_val[0])){
update_post_meta($post_id,'_wpcf_access_group','');
}
}
One other quick question - when content is part of a post group which non-registered users cannot see (no read permissions) is there a way to customise the message that they are shown. So, instead of a 404 or 'permission denied' message they can be redirected to a login page?
One last thing - the content is now properly restricted, however, views are showing restricted content. Is there a way to hide restricted content in a view depending on post group access? i.e. can the View check for access rights before showing content?
The usage case is that some of this content will be of a sensitive nature, and shouldn't be available for the public. This is currently working, using the private field and post groups. However, the View still shows the title and intro text for that post, even though one cannot click through to read it unless one is logged in.
Your code is working when a post is saved, but not when one is created or edited with a CRED post form. Is it possible amend it to make that work? All content on this site will be created that way.
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']==12)
{
// add your code here to test the post's terms or custom field values
$field_val = get_post_meta($post_id, 'wpcf-keep-private');
// and apply the correct post group
if ( $field_val[0] == '1'){
update_post_meta($post_id,'_wpcf_access_group','wpcf-custom-group-2c17c6393771ee3048ae34d6b380c5ec');
}
if ( empty($field_val[0])){
update_post_meta($post_id,'_wpcf_access_group','');
}
}
}
Please try this and let me know if it helps.
Thanks,
Shane