I'm trying for hours now to find the error moving back and forth. I have a custom post type. Everything works fine until I add my custom meta box. Now suddenly the slug gets the title of one of $post->ID == '21'
child pages instead its own title when creating a new custom post. It always is the title from the page with the lowest $post->ID
. But when I remove my custom meta box again. Everything works fine. I may have tracked it down to the foreach
but I can't see what's wrong there.
Besides this %postname%
error everything's fine. The custom field gets saved correctly. I even can edit the slug manually. But that's not how it is supposed to be. Please help!
Tried it without the <form>
tags now. Problem still persists.
Calling wp_reset_postdata()
didn't work as well. But I realized the slug always gets created right after I fill the title text input. Also when I edit the custom post later and change some content the slug gets wrong again.
add_action( 'init', 'cpt_init_cases' );
function cpt_init_cases() {
register_post_type( 'case', array(
'labels' => array(
'name' => __( 'Cases' ),
'singular_name' => __( 'Case' )),
'description' => 'Create cases to be displayed on single customer child pages',
'public' => true,
'menu_position' => 6,
'rewrite' => array('slug' => 'case'),
'supports' => array('title', 'editor', 'thumbnail', 'custom-fields')
));
}
/*** when I delete everything below this line slugging works as expected ***/
add_action('admin_init', 'admin_init');
function admin_init(){
add_meta_box('refInfo-meta', 'Reference', 'reference_meta_callback', 'case', 'side', 'high');
}
function reference_meta_callback( $post ){
global $post;
$custom = get_post_custom($post->ID);
if( isset($custom['reference_id'][0]) ) {
$reference_id = $custom['reference_id'][0];
} else {
$reference_id = '0';
}
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<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
$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
}
add_action( 'save_post', 'save_reference_id' );
function save_reference_id( $post_id ) {
global $post;
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
if( isset($_POST['ref_id']) ) {
update_post_meta($post->ID, 'reference_id', $_POST['ref_id']);
}
}