Skip Navigation

[Resolved] Check array of checkbox field values

This support ticket is created 5 years, 8 months 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by davidS-53 5 years, 8 months ago.

Assisted by: Waqar.

Author
Posts
#1227818

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!

#1227867

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

#1231327

My issue is resolved now. Thank you!