Skip to navigation Skip to content
OTGS Knowledge Base
  • Login
  • Home
  • Add Code Snippets
  • Add Compatibility Snippet
  • Add Content
  • Add Content – Subscriber
  • Add Developer Resource
  • Add Quick Texts
  • Add Supporter Resource
  • Add Trainings
  • Add Webinars
  • All Courses
  • Contractor courses
  • Courses
  • Log In
  • Log Out
  • My Courses
  • New Request
  • Newly added resources
  • Privacy Policy and GDPR Compliance
  • Register
  • Reset Password
  • Set Password
  • Trainings

Get Checkboxes values with get_post_meta

Usually, Custom Fields are single values. Not so Checkboxes. Those are arrays.

Below Code lets you var_dump and get_post_meta() each stored value

 

 

This is the vardump for the whole Checboxes Field:<br>
<?php
  $checkboxes = get_post_meta($post->ID, 'checkboxes');
  var_dump($checkboxes);
  ?><br>
  This is the above's array vardump<br>
  <?php
  foreach ($checkboxes as $checkbox => $value) {
  var_dump($value);
    foreach ($value as $saved) {
    ?><br>This is the single saved value<br><?php
    var_dump($saved);
      foreach ($saved as $bare_value) {
        ?><br>This is the single echoed value<br><?php
        var_dump($bare_value);
      }
    }
   }

This is the same for Generic CRED Checkboxes and Types Checkboxes.

Above code vardumps the values, of course you can echo them, or do whatever else you like with it.

It might come in handy when you need to get values from one checkboxes field and update another field with this values.

Mind you, in a ShortCode to return the above, you must return the values, not echo, and since it can have many values, you need to append the string to the returned value.
Also you need to move the Return statement out of the Foreach’s.

This below example will output all saved values as a shortcode (useful for generic Fields)

function saved_checkboxes_values( $atts ){
    global $post;

    $checkboxes = get_post_meta($post->ID, 'checkboxes');
    $values = '';

    foreach ($checkboxes as $checkbox => $value) {


        foreach ($value as $saved) {


            foreach ($saved as $bare_value) {


                $values .= '<li>'.$bare_value.'</li>';
                

            }

            
        }

    }
    return $values;
}
 add_shortcode( 'saved-checkboxes-values', 'saved_checkboxes_values' );

Then you can use it as this:

[saved-checkboxes-values]


Let us know if this snippet is not working for you:

This snippet doesn’t work
Tags: checkboxes, get_post_meta

Post navigation

Previous post: Redirect Old Domains
Next post: Group Posts by Date (year and month)
0 0 votes
Article Rating
Subscribe
Notify of
guest

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
OnTheGoSystems - One team, one goal
0
0
Would love your thoughts, please comment.x
()
x
| Reply

Report Post

« »

 

Your Name:

Your Email:

Please tell us why do you think this post is inappropriate and shouldn't be there:


Cancel Report