Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to reference posts to each other. Editors should have a custom field select list of certain post's titles to link the current post with other posts. I got quite far, I think. The select list appears correctly. All titles are there.

Now, I'm stuck at the point, where the referred post's ID has to be saved.

How do I grab the selected option's value, save it and make it default value? Do I have to wrap everything inside a form function?

This is what I have so far:

add_action("admin_init", "admin_init");
add_action('save_post', 'save_reference_id');  
/*** add_meta_box ***/
function admin_init(){
    // adding a custom field to post type 'case'
    add_meta_box("refInfo-meta", "Reference", "meta_options", "case", "side", "high");
}  
/*** callback ***/
function meta_options(){
  ?>
    <form action="<?php bloginfo('url'); ?>" method="get">
      <select name="page_id" id="page_id">

      <?php
      global $post;
      // getting all child pages of ID 21
      $args = array( 'numberposts' => -1, 'post_type' => 'page', 'post_parent' => 21);
      $posts = get_posts($args);

      foreach( $posts as $post ) : setup_postdata($post); ?>
        <option value="<?php echo $post->ID; ?>"><?php the_title(); ?></option>
      <?php endforeach; ?>

      </select>
    </form>

  <?php
}  
/*** save_post ***/
function save_reference_id(){
  // big question mark
}
share|improve this question
    
have you looked at the example on add_meta_box? also, don't use page_id, that's a WordPress query var, always use unique prefixes. –  Milo Jun 24 at 15:10

1 Answer 1

selected() was big help for setting a default value. The rest I found in this brilliant meta box tutorial: http://code.tutsplus.com/tutorials/how-to-create-custom-wordpress-writemeta-boxes--wp-20336 with examples for text input, checkboxes and dropdown. Also Custom post type's slug gets wrong when adding a custom meta box explained me how to correctly handle the current post object so it doesn't get mixed up with the options objects.

/*** callback ***/
function meta_options(){
  global $post;
  // storing the global post object so it doesn't get mixed up with the options
  $post_old = $post

  $custom = get_post_custom($post->ID);
  if (isset($custom["reference_id"][0])) {
    $reference_id = $custom["reference_id"][0];
  } else {
    $reference_id = '0';
  }
  ?>
    <form action="<?php bloginfo('url'); ?>" method="get">
      <select name="ref_id" id="ref_id">
        <option value="0" <?php selected($reference_id, '0'); ?>>- choose client -</option>
      <?php
      global $post;
      $args = array(
        'numberposts' => -1,
        'post_type' => 'page',
        'post_parent' => 21
        );
      $posts = get_posts($args);
      foreach( $posts as $post ) : setup_postdata($post); ?>
        <option value="<?php echo $post->ID; ?>" <?php selected($reference_id, $post->ID); ?>><?php the_title(); ?></option>
      <?php endforeach; ?>
      </select>
    </form>
  <?php
  // restoring the global post object
  $post = $post_old;
  setup_postdata( $post );
}

/*** save_post ***/
function save_reference_id(){
  global $post;
  if (isset($_POST["ref_id"])) {
    update_post_meta($post->ID, "reference_id", $_POST["ref_id"]);
  }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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