I have a checkboxes field with a range of checkboxes with a number stored as the value. Field slug is wpcf-page-access.
In my functions.php, I'm getting the array of checked items from the field like this:
$pageaccess = get_post_meta( $userid, 'wpcf-page-access', true);
What I need to do is check if a specific value exists in that array.
This is basically where I've gotten to (but the if statement doesn't appear to be working- guess the data in the array isn't correct):
$option= "1";
$pageaccess = get_post_meta( $postid, 'wpcf-page-access', true);
if ( in_array( $option, $pageaccess ) ) {
// "1" is in the array of checked values
}
Hopefully that makes sense. Cheers!
Hi David,
Thank you for waiting.
In my tests, getting the custom field value through the Toolset Fields API ( https://toolset.com/documentation/customizing-sites-using-php/functions/#checkboxes ) and then splitting it into an array works:
$option = "1";
$pageaccess = types_render_field( "page-access", array( "separator" => ", " ) );
$pageaccess_arr = explode(', ', $pageaccess);
if (in_array($option, $pageaccess_arr)) {
// "1" is in the array of checked values
}
I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar
My issue is resolved now. Thank you!