I am trying to code the ability to pick which posts should be included as related to the current post the user is editing. I have used @MikeSchinkel very helpfull post here, to include a list of posts in a metafield, I have then used this patch to make the option multi select-able, I have also used the jquery chosen plugin to tidy the list up slightly. I am now thoroughly stuck on how to retrieve the values in my loop on the front end, i've tried the following amongst other things:

$intro = get_post_meta( $post->ID, 'SELECT_POST_TYPE', true );
echo $intro;
var_dump( $_POST)

and this:

$intro = get_post_meta( $post->ID, $selected_post_id, true );
echo $intro;

and this:

$intro = get_post_meta( $post->ID, 'select_box', true );
echo $intro;

the code to register the meta_box is the following:

class WPSE_85107 {
  var $FOR_POST_TYPE = 'question';
  var $SELECT_POST_TYPE = 'post';
  var $SELECT_POST_LABEL = 'Post';
  var $box_id;
  var $box_label;
  var $field_id;
  var $field_label;
  var $field_name;
  var $meta_key;
  function __construct() {
    add_action( 'admin_init', array( $this, 'admin_init' ) );
  function admin_init() {
    add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
    add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
    $this->meta_key     = "_selected_{$this->SELECT_POST_TYPE}";
    $this->box_id       = "select-{$this->SELECT_POST_TYPE}-metabox";
    $this->field_id     = "selected-{$this->SELECT_POST_TYPE}";
    $this->field_name   = "selected_{$this->SELECT_POST_TYPE}";
    $this->box_label    = __( "Select {$this->SELECT_POST_LABEL}", 'wpse-85107' );
    $this->field_label  = __( "Choose {$this->SELECT_POST_LABEL}", 'wpse-85107' );
  function add_meta_boxes() {
      array( $this, 'select_box' ),
  function select_box( $post ) {
    $selected_post_id = get_post_meta( $post->ID, $this->meta_key, true );
    global $wp_post_types;
    $save_hierarchical = $wp_post_types[$this->SELECT_POST_TYPE]->hierarchical;
    $wp_post_types[$this->SELECT_POST_TYPE]->hierarchical = true;
    wp_dropdown_pages( array(
      'id' => $this->field_id,
      'name' => $this->field_name,
      'selected' => empty( $selected_post_id ) ? 0 : $selected_post_id,
      'post_type' => $this->SELECT_POST_TYPE,
      'show_option_none' => $this->field_label,
      'multiselect' => true,
      'class' => 'chzn-select'
    $wp_post_types[$this->SELECT_POST_TYPE]->hierarchical = $save_hierarchical;
  function save_post( $post_id, $post ) {
    if ( $post->post_type == $this->FOR_POST_TYPE && isset( $_POST[$this->field_name] ) ) {
      update_post_meta( $post_id, $this->meta_key, $_POST[$this->field_name] );
new WPSE_85107();

UPDATE: thanks to @matt_ here is the anwser this code goes in your front end loop-template:

 $meta = get_post_meta($post->ID, '_selected_post', true); 

                        if ( ! $meta ):
                            echo  '';

                        <a class="related_post" href="<?php echo get_permalink($meta); ?>"><?php echo get_the_title($meta); ?></a>
                    <?php  endif;
  • Please rework that code styling. Half didn't work out, half is not readable. Thanks
    – kaiser
    Commented Jul 12, 2013 at 18:42
  • thank you for taking the time to look i have edited it...
    – vimes1984
    Commented Jul 12, 2013 at 18:45
  • Are you certain your meta is being saved? Try taking a look at the database. Not entirely sure what your question is.
    – GhostToast
    Commented Jul 12, 2013 at 20:27
  • @ghosttoast my question here is how do i retrieve the metabox value inside my custom post loop template file (archive-questions.php) on the front end.
    – vimes1984
    Commented Jul 12, 2013 at 21:21

1 Answer 1


Looks like you will have to use

$meta = get_post_meta($post->ID, '_selected_post', true); echo $meta;

The registered post type is hidden using the _ and it's name is defined at the line :

$this->meta_key = "_selected_{$this->SELECT_POST_TYPE}";

Where SELECT_POST_TYPE is defined as 'post' at the line :

var $SELECT_POST_TYPE = 'post';

Hope it helps.

  • yes that does help I understand it now, I don't think it's doing quite what i want any way. It's just returning the post id not the title. On top of that i am also trying to retrive the post url... I'm building another function all is not going well though it's a sharp learning curve..
    – vimes1984
    Commented Jul 13, 2013 at 13:50
  • Then you just maybe have to use echo get_the_title($meta); where you need to display it. Do you accept the response if it at least helps you? Thanks.
    – Mat_
    Commented Jul 13, 2013 at 17:38
  • That works perfectly Thank you so much @matt_ I have been pulling my hair out for the last two days over this!!! I only have one problem left it only returns the first value i have selected from the multi select box do you know how to force it to retrive all the values selected or maybe register another select box in the same meta_box.
    – vimes1984
    Commented Jul 13, 2013 at 17:55
  • Hi. If, in the line $meta = get_post_meta($post->ID, '_selected_post', true); echo $meta; you remove the true, and you don't echo the result but put it in a var_dump($meta);, what result do you have?
    – Mat_
    Commented Jul 14, 2013 at 14:02
  • @matt_ thanks for the help I have just registered 4 different buttons in the end. It works for now I spent to long on this allready! Thanks again for taking the time with this. Chris
    – vimes1984
    Commented Jul 16, 2013 at 12:17

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.