Skip Navigation

[Resolved] Checkboxes Title and Stored value

This support ticket is created 4 years 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 2 voices.

Last updated by Christian Cox 4 years ago.

Assisted by: Christian Cox.

Author
Posts
#1879329

For a php function I need to retrieve both the Title and the Stored Value of each checkbox

hidden link

Is this possible?

This is the snippet, but I would need a way to get the title (????????)

$feats = get_post_meta(get_the_ID(), 'wpcf-obj-amenities', true);
foreach ($feats as $feat) {
	$feat_stored = $feat[0];
	$feat_title =  ????????;
    $featstext = $featstext  . 'Title: ' . $feat_title  . ' - Stored Value: ' . $feat_stored . '<br>';
}
#1879707

Hello, it looks like you want to find the checkbox title that corresponds to a postmeta value stored by that checkbox. That type of API query doesn't exist in Toolset, but you can use the Types Field API types_render_field to get the selected checkbox titles and values from a given post, then split those out into arrays like so:

// get comma-separated strings of the raw values and titles of each checked checkbox
$the_id = 12345; // post id
$cbs_raw = types_render_field("obj-amenities", array('item' => $the_id, 'output' => 'raw', 'separator' => ','));
$cbs_titles = types_render_field("obj-amenities", array('item' => $the_id, 'separator' => ','));
//error_log($cbs_raw);
//error_log($cbs_titles);

// split those strings by the comma character into arrays of strings
$cbs_raw_array = explode(',', $cbs_raw);
$cbs_titles_array = explode(',', $cbs_titles);
//error_log(print_r($cbs_raw_array, true));
//error_log(print_r($cbs_titles_array, true));

If the error logs are uncommented, they produce the following output for your reference:

[17-Dec-2020 20:16:03 UTC] 2,3
[17-Dec-2020 20:16:03 UTC] Checkbox title 2,Checkbox title 3
[17-Dec-2020 20:16:03 UTC] Array
(
    [0] => 2
    [1] => 3
)

[17-Dec-2020 20:16:03 UTC] Array
(
    [0] => Checkbox title 2
    [1] => Checkbox title 3
)

Then you could loop over one of those arrays to produce your own custom output including the title and value of each checked checkbox:

for($i=0; $i<sizeof($cbs_raw_array); $i++){
  $string = $string . 'Title: ' . $cbs_titles_array[$i] . ' - Stored Value: ' . $cbs_raw_array[$i] . '<br />';
}
#1885093

I still get the same value for $cbs_raw and $cbs_titles.
Both show the raw value (slug).

What can it be?

Here the complete function I am using:

add_shortcode('list-icons-amenities', 'list_icons_amenities');
function list_icons_amenities() {
	
	// set variables
	$path = get_site_url();
	$feats_table = '<div class="features-table">';
	$pre = '<div class="features-box" style="display:inline-block; width:100px; margin-bottom:25px;">';
	$pre_label = '<div class="features-label" style="font-size:8px; text-align:center; margin-bottom:2px;">';
	$pre_icon = '<div class="features-icon" style="font-size:8px; text-align:center;" title="';
	$mid_icon = '"><img src="' . $path . '/wp-content/uploads/icons/';
	$suf_icon = '.png"/></div>' ;
	$suf = '</div>';
	
	// get comma-separated strings of the raw values and titles of each checked checkbox
	$the_id = get_the_ID(); // post id
	$cbs_raw = types_render_field("obj-amenities", array('item' => $the_id, 'output' => 'raw', 'separator' => ','));
	$cbs_titles = types_render_field("obj-amenities", array('item' => $the_id, 'separator' => ','));
	 
	// split those strings by the comma character into arrays of strings
	$cbs_raw_array = explode(',', $cbs_raw);
	$cbs_titles_array = explode(',', $cbs_titles);

	for($i=0; $i<sizeof($cbs_raw_array); $i++){
	  $feats_table = $feats_table . $pre . $pre_label . $cbs_titles_array[$i] . '</div>' . $pre_icon . $cbs_titles_array[$i] . $mid_icon . $cbs_raw_array[$i] . $suf_icon . $suf ;
	}
		
$feats_table = $feats_table . '</div>';	
	
return $feats_table;
}

Here is what I store in the checkboxes

hidden link

and here the result

hidden link

#1888775

I would need to see the error logs generated by these log statements. Can you turn on error logging and add those error logs statements to your code?

$cbs_raw = types_render_field("obj-amenities", array('item' => $the_id, 'output' => 'raw', 'separator' => ','));
$cbs_titles = types_render_field("obj-amenities", array('item' => $the_id, 'separator' => ','));
error_log($cbs_raw);
error_log($cbs_titles);

Then copy the logs and paste them into your next reply.

#1888873

Hi, together with Jamal we have investigated this issue and found out that this is a Toolset's bug.

https://toolset.com/forums/topic/get-all-custom-fields/

Jamal already escalated this to 2nd Tier.

Thanks

#1888913

Okay thanks, I'll close here since there is another open ticket.