Although not listed in the following resources, seems that parent_id
is a reserved name in $_POST
and $_REQUEST
:
This code works in a Woocommerce product page to show a dropdown of regular pages. Also, in your code, you were missing the get_post_meta
to fill the selected
argument value.
add_action( 'add_meta_boxes','add_metabox_wpse_83542' );
add_action( 'save_post', 'save_post_wpse_83542', 10, 2 );
function add_metabox_wpse_83542()
{
add_meta_box(
'postparentdiv',
__('Page Parent'),
'meta_box_content_wpse_83542',
'product',
'side',
'core'
);
}
function meta_box_content_wpse_83542( $post )
{
$meta = get_post_meta( $post->ID, '_parent_id', true );
$selected = ( isset( $meta ) ) ? $meta : '';
$dropdown_args = array(
'post_type' => 'page',
'exclude_tree' => $post->ID,
'selected' => $selected,
'name' => '_parent_id',
'show_option_none' => __( '(no parent)' ),
'sort_column' => 'menu_order, post_title',
'echo' => 0,
'child_of' => 97, // Sales Page
);
$dropdown_args = apply_filters( 'page_attributes_dropdown_pages_args', $dropdown_args, $post );
$pages = wp_dropdown_pages( $dropdown_args );
if ( ! empty($pages) )
{
wp_nonce_field( plugin_basename( __FILE__ ), 'noncename_wpse_83542' );
?>
<p><strong><?php _e('Parent') ?></strong></p>
<label class="screen-reader-text" for="parent_id"><?php _e('Parent') ?></label>
<?php
echo $pages;
} // end empty pages check
}
function save_post_wpse_83542( $post_id, $post )
{
// Block on Autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// Block Revisions
if ( 'revision' == $post->post_type )
return;
// Nonce verify
if ( !wp_verify_nonce( $_POST['noncename_wpse_83542'], plugin_basename( __FILE__ ) ) )
return;
if ( $post->post_type == 'product' && !empty( $_POST['_parent_id'] ) )
{
update_post_meta( $post->ID, '_parent_id', $_POST['_parent_id'] );
}
}