Skip to navigation Skip to content
OTGS Knowledge Base
  • Login
  • Home

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]

Tags: checkboxes, get_post_meta

Update post title/name with a Date from a Types Date CF

Update post title/name with a Date taken from a types Date CF (and obviously you won’t 2664748859 as title but 11-06-2014 or similar) with CRED hook. Remember that Toolset uses Timestamp but you want a “normal” date as Post Title.

function ttd_save_data_action($post_id, $form_data){
            // Change your CRED Form "ID" accordingly below
            if ($form_data['id']==ID){
               
                //Declare the content of your variables, change "your_custom_field_slug" accordingly
                $custom_value = get_post_meta( $post_id, 'wpcf-your_custom_field_slug', true );//get posts' Date field value (timestamp)
                $custom_title = gmdate("m-d-Y", $custom_value);//convert timestamp to m-d-y for output
                //collect data and define new title
                $my_post = array(
                    'ID'               => $post_id,
                    'post_title'   => $custom_title,
                    'post_name' => $custom_title,
                   
                );
               
                // Update the post into the database
                wp_update_post( $my_post );
               
            }
        }
    add_action('cred_save_data', 'ttd_save_data_action',10,2);
Tags: database, front-end admin, get_post_meta, update_post_meta
OnTheGoSystems - One team, one goal

Report Post

« »

 

Your Name:

Your Email:

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


Cancel Report