Skip Navigation

[Resolved] Dynamic Post Groups based on field values

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

Problem:

The issue here is that the user wanted to dynamically allocate a custom post group to their post based on a checkbox field value.

Solution:

This can be done by following the instructions in the link below.

https://toolset.com/forums/topic/dynamic-post-groups-based-on-field-values/#post-1160728

This support ticket is created 6 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 13 replies, has 2 voices.

Last updated by Ian Henderson 6 years, 1 month ago.

Assisted by: Shane.

Author
Posts
#1158756

Hi Christian - I'm sorry but it's taken me a long time to get to this. I am following up on this thread: https://toolset.com/forums/topic/split-dynamic-post-groups-based-on-field-or-taxonomy-values/

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

- and then add this to functions.php?

#1158878

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Ian,

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

#1159654

Hi 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_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 == '1'){
update_post_meta($post_id,'_wpcf_access_group','wpcf-custom-group-2c17c6393771ee3048ae34d6b380c5ec');
}
}

#1159972

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Ian,

Would you mind allowing me to have admin access to the website so that I can test this out for you?

The private fields will be enabled for your next response.

Thanks,
Shane

#1159975

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Private fields

#1160095

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Ian,

I've adjusted the code and it should be working now.

Thanks,
Shane

#1160484

Thanks Shane - yes, it's working! What line needs to be added to remove the post from the group if the field is then unticked?

#1160728

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Ian,

So currently the code we have now is 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');
	}
}

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

Please let me know if this helps.
Thanks,
Shane

#1160729

Works like a charm, thanks Shane.

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?

#1160834

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Ian,

I believe that you can create a custom page in Layouts that you can use as your error page and add a login form to it.

https://toolset.com/documentation/user-guides/setting-access-control/#selecting-error-display-for-archive-pages

Please let me know if this helps.

Thanks,
Shane

#1162675

Thanks Shane - I've sort that problem out.

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.

thanks

#1163917

Hi Shane

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.

thanks
Ian

#1163928

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Ian,

FOr it to work on your CRED form you just need to use this hook
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

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

#1164581

My issue is resolved now. Thank you!