I have created a custom post type (cpt) and within each post type is a custom meta field.

This custom meta field displays all the pages that can 'checked'.

Code to generate the custom meta box:

// display list of pages with check box
function page_meta_box( $post ) {

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

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

$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, $checkfield) ) { ?>checked="checked" <?php } ?>/>
          <label for="page_<?php echo $page->ID; ?>"><?php echo  $page->post_title; ?></label><br>

Code to save the custom meta box values:

//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, 'checkfieldBox', $_POST['checkfield'] );

The query I am using to display the custom post types on the front end:

<?php $query = new WP_Query( array(
                'post_type' => 'sidebar_boxes',
                'posts_per_page' => -1,
                'orderby' => 'menu_order', 
                'order' => 'ASC'
            ) );

    while ( $query->have_posts() ) : $query->the_post(); ?>
        <div class="sidebarBox floatLeft">
            <?php if (has_post_thumbnail())
            { ?>
                    <?php the_post_thumbnail();?>
            } ?>
            <div class="sideBoxText">
                <?php // Content here ?>
wp_reset_query(); ?>    

The above code displays the CPTs with no problems.

My issue is how do I display the custom post type on the page that has been checked. Currently they are all appearing.

E.g. If I check the 'about' page on the CPT page in the dashboard, how can I display that information on the about page (front end)

Any help much appreciated.

  • As a sidenote, you shouldn't need maybe_unserialize() because passing true to the get_post_meta() function will already unserialize any serialized data. get_post_meta() runs get_metadata() and if passed bool true will also run maybe_unserialize()
    – Howdy_McGee
    Nov 17, 2015 at 17:05
  • Thank you. I must admin i've never seen maybe_serialize() before. In face I always use get_post_meta.
    – rikardo85
    Nov 17, 2015 at 17:20

1 Answer 1


You just want a meta_query based on the current page's ID:

$query = new WP_Query( 
    'post_type' => 'sidebar_boxes',
    'posts_per_page' => -1,
    'orderby' => 'menu_order', 
    'order' => 'ASC',
    'meta_query' => array(
      'key' => 'checkfieldBox',
      'value' => $post->ID,

You can really only depend upon $post in the way I did on "singular" pages and only if the the $post object hasn't been altered by code on the page. If you have trouble you will need to use:

$post_obj = get_queried_object();
// now use $post_obj->ID instead of $post->ID
  • Thank you. Can you possibly point me in the right direction on how to display the posts on the pages that have been checked?
    – rikardo85
    Nov 17, 2015 at 17:20
  • That is exactly what I just did, but your description is confusing.
    – s_ha_dum
    Nov 17, 2015 at 17:43

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.