I have a custom post type called Testimonies. What I'm trying to do is allow a user to create a new Testimony and then select from a list of checkboxes which page(s) on the site he wants this testimony to display on.

I have managed to create the checkboxes to list all the site pages and code is below:

add_action("admin_init", "admin_init3");

function admin_init3(){ 
add_meta_box("credits_meta", "Choose on which pages to display",     "myplugin_inner_custom_box", "testimonies", "normal", "low");

function myplugin_inner_custom_box( $post ) {
// we store data as an array, we need to unserialize it
$checkfield = maybe_unserialize( get_post_meta($post->ID, "checkfield", true) );

// Nonce to verify intention later
wp_nonce_field( 'save_quote_meta', 'custom_nonce' ); 

$pages = get_pages(); 
foreach ( $pages as $page ) { ?>
    <input id="page_<?php echo $page->ID; ?>" type="checkbox" name="checkfield[]" value="<?php echo $page->ID; ?>" <?php if ( in_array($page->ID, (array) $checkfield) ) { ?> checked <?php } ?>/> <label for="page_<?php echo $page->ID; ?>"><?php echo $page->post_title; ?></label> <br>
//save the meta box action
add_action( 'save_post', 'myplugin_meta_save', 10, 2 );

//save the meta box
function myplugin_meta_save($post_id, $post)
if ( isset($_POST['checkfield']) ) { // if we get new data

    update_post_meta($post_id, "checkfield", $_POST['checkfield'] );


However, I'm having difficulty with the next step. My idea is to put the checkbox values in a variable, then check whether the current page id matches one of these values. If it does, I'll display the custom post. This is all a bit hazy though, and I'm not sure if I'm going about it the right way.

Currently, I'm struggling to pass the checkbox values into a variable and echo these out. I have the below code but it's just echoing array, array, array, array.

<?php  $checkboxes=get_post_meta($post->ID, "checklist", false);
if ($checkboxes) {
foreach($checkboxes as $checkbox) {
echo  $checkbox;
} ?>

Any help would be much appreciated.

Many thanks.

1 Answer 1


It sounds like you need to run a foreach on a value inside the array. E.g. $checkboxes['value'] as $checkbox.. If you do: print_r('$checkboxes') you'll get an idea of the values you are trying to target. You might even have to do something like $checkboxes['something']['something_else'] depending on how many levels the array has.

Hope it helps, do a print_r and view source, or wrap it in pre tags I'm sure you'll figure out how to target the foreach then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.