$post->;ID未在中工作metabox_callback
添加自定义查询后。我如何避免这种情况?我应该使用$post_id = isset( $_GET[\'post\'] ) ? $_GET[\'post\'] : ( isset( $_POST[\'post_ID\'] ) ? $_POST[\'post_ID\'] : false );
而不是$post->;身份证件
类似问题/相同情况here.
出现此类问题的原因是什么?如何以正确的方式解决?
EDIT:
function custom_add_meta_boxes() {
add_meta_box (
\'enroll_user\',
\'Enroll users\',
\'enroll_user_callback\',
\'group\',
\'normal\'
);
add_meta_box (
\'module_group\',
\'Assign modules\',
\'module_group_callback\',
\'group\',
\'side\'
);
}
add_action( \'add_meta_boxes\', \'custom_add_meta_boxes\' );
function enroll_user_callback ( $post ) {
wp_nonce_field( \'enroll_user_save\', \'enroll_user_nonce\' );
echo \'<label for="enroll_user_field">Enroll users</label><br /><br />\';
echo \'<select style="width: 50%; min-width: 250px;" multiple name="enroll_user_field[]" id="enroll_user_field">\';
$users = get_users( array( \'fields\' => array( \'ID\', \'user_email\' ) ) );
foreach( $users as $user ){
$enrolledusers = get_user_meta($user->ID, \'_enroll_user_value_key_\' . $post->ID , false);
if( in_array($post->ID, $enrolledusers )) {
echo \'<option selected value="\' . $user->ID . \'">\' . esc_attr( $user->user_email ) . \'</option>\';
} else {
echo \'<option value="\' . $user->ID . \'">\' . esc_attr( $user->user_email ) . \'</option>\';
}
}
echo \'</select>\';
// This is posting a different ID after adding my query in module_group_callback
print_r($post);
}
function module_group_callback ( $post ) {
wp_nonce_field( \'module_group_save\', \'module_group_nonce\' );
echo \'<label for="module_group_field">Assign modules</label><br /><br />\';
$args = array(
\'post_type\' => \'module\',
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'posts_per_page\' => -1,
);
$query = new WP_Query( $args );
echo \'<select style="width: 100%;" multiple name="module_group_field[]" id="module_group_field">\';
while ( $query->have_posts() ) : $query->the_post();
$moduleid = $query->post->ID;
echo \'<option value="\' . $moduleid . \'">\' . esc_attr( get_the_title( $moduleid ) ) . \'</option>\';
endwhile;
echo \'</select>\';
}
最合适的回答,由SO网友:Sally CJ 整理而成
那个$query->have_posts()
修改全局$post
变量,该变量也用于管理端和站点的前端/公共端,您只需调用wp_reset_postdata()
恢复全局$post
在你打电话给$query->have_posts()
.
但在管理方面,您需要在循环结束后手动恢复变量,如下所示:
function module_group_callback( $post ) {
...
while ( $query->have_posts() ) : $query->the_post();
...
endwhile;
// Restore the global $post back to the one passed to your metabox callback.
$GLOBALS[\'post\'] = $post;
}
但如果你打电话
global $post
在代码中,则可以使用备份方法:
function module_group_callback( $post ) {
global $post; // this is required
$_post = $post; // backup the current $post variable in the global scope
...
while ( $query->have_posts() ) : $query->the_post();
...
endwhile;
$post = $_post; // restore
}
或者,您可以省略
$query->have_posts()
并手动循环通过
$query->posts
:
function module_group_callback( $post ) {
...
// Don\'t use $post with the \'as\'. Instead, use $p, $post2 or another name.
foreach ( $query->posts as $p ) {
echo \'<option value="\' . $p->ID . \'">\' . esc_attr( get_the_title( $p ) ) . \'</option>\';
}
}
无论哪种方式,如果您的代码修改了全局
$post
, 然后确保稍后恢复。
顺便说一句,你想使用esc_html()
那里,因为esc_attr()
用于转义属性值,例如。<input value="<?php echo esc_attr( \'some <b>HTML</b>\' ); ?>">
. :-)