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
}
add_meta_box
? also, don't usepage_id
, that's a WordPress query var, always use unique prefixes.