Skip Navigation

[Résolu] Check array of checkbox field values

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

This topic contains 2 réponses, has 2 voix.

Last updated by davidS-53 Il y a 2 années et 5 mois.

Assigned support staff: Waqar.

Auteur
Publications
#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

Waqar
Supporter

Languages: Anglais (English )

Timezone: Asia/Karachi (GMT+05:00)

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!