I need to customize the access of unregistered users to a custom post type based on the value of a custom field. If my custom field is smaller than 50, the custom post type must not be accessible to unregistered users (but only to registered users), while if the custom field is larger than 50, the custom post type must be accessible to unregistered users as well. I am using Toolset with Layouts. The post type has multiple Content Templates organized in a Layout.
I know I can do this by blocking the contents using conditional statements in the Content Templates, but this is a bit messy.
Is there a filter acting directly on Layouts or Access or else that I could use to do this in functions.php?
Thanks very much.
Hi,
Welcome to Toolset support. There is no out-of-the-box solution when it comes to your request. Maybe it is possible with custom coding which is outside of our support scope.
One idea can be to use the Toolset Access API:
https://toolset.com/documentation/programmer-reference/access-api-filters/
For example, you could hook into toolset_access_api_get_post_permissions to modify the read permission based on the custom field value. Here’s a sample snippet you might add as a custom code:
add_filter( 'toolset_access_api_get_post_permissions', 'my_custom_post_access', 10, 5 );
function my_custom_post_access( $has_permission, $post, $permission_type, $user, $language_code ) {
// Only affect 'read' permission for your custom post type and unregistered users.
if ( 'read' === $permission_type && ! is_user_logged_in() && 'your_custom_post_type' === get_post_type( $post ) ) {
// Replace 'your_custom_field' with the actual custom field key.
$custom_value = get_post_meta( $post->ID, 'your_custom_field', true );
if ( isset( $custom_value ) && floatval( $custom_value ) < 50 ) {
// Block access if the custom field value is less than 50.
$has_permission = false;
}
}
return $has_permission;
}
The filter intercepts permission checks for posts. It verifies that the permission type is for reading, the post is of your custom post type, and the user is not logged in.It then checks the custom field value. If it’s below 50, it sets the permission to false, effectively preventing access. You can adjust the logic to suit your specific needs without embedding conditionals directly in your Layouts.
Please Note: The code above is presented as a sample on what can be achieved with Toolset Access API, but it does not guarantee a working solution. It is considered as a starting point that you can expand your logic on. If you need additional code related help, you can consider hiring a professional:
https://toolset.com/contractors/
Hi,
Thanks. I tried what you propose as well as various possibilities to include this bit of code inside add_action functions for wp, the_post, pre_get_posts, and template_redirect hooks, but with no success.
It seems to me that this access hook allows to retrieve the status of $has_permission, but it does not allow to change it.
Are you sure that this hook allows to change the permission status of a post? If yes, when should it be triggered to work properly?
In the end, I just made a redirect in a function myfunction called by the add_action( 'template_redirect', 'myfunction') hook. It'd be nice to be able to do this directly with the Access API. Thanks for your help.