为什么我的查询会干扰全局POST变量?

时间:2017-10-10 作者:ShoeLace1291

我正在开发一个插件。有两个CPT:类别和菜肴。在我的编辑盘页面中,我尝试使用get_post_meta($post->ID, \'dish_price\', true) 问题是,我用来获取正在编辑的菜肴ID的任何函数都会返回在类别下拉列表中检索到的最后一个类别的ID。我做错了什么?

function meta_boxes(){
    /*Category meta boxes */
    remove_meta_box(\'commentstatusdiv\', \'category\', \'normal\');
    remove_meta_box(\'commentsdiv\', \'category\', \'normal\');
    remove_meta_box( \'postexcerpt\', \'category\', \'core\' );
    add_meta_box( \'description\', \'Description\', \'category_description_meta_box\', \'category\' );
    /* Dish meta boxes */
    remove_meta_box(\'commentstatusdiv\', \'dish\', \'normal\');
    remove_meta_box(\'commentsdiv\', \'dish\', \'normal\');
    remove_meta_box( \'postexcerpt\', \'dish\', \'core\' );
    add_meta_box( \'parent_id\', \'Category\', \'dish_category_metabox\', \'dish\' );
    add_meta_box( \'description\', \'Description\', \'dish_description_meta_box\', \'dish\' );
    add_meta_box(\'dish_price\', \'Price\', \'dish_price_meta_box\', \'dish\');
}

function category_description_meta_box(){
    global $post;
    //var_dump($post)
    echo \'<input type="hidden" name="category_noncename" id="category_noncename" value="\' .wp_create_nonce( plugin_basename(__FILE__) ) . \'" />\';

    echo \'<textarea style="width: 100%;" rows="5" name="description" id="description">\'.get_post_meta($post->ID, \'description\', true).\'</textarea>\';
}

function dish_description_meta_box(){
    global $post;
    //var_dump($post)
    echo \'<input type="hidden" name="dish_noncename" id="dish_noncename" value="\' .wp_create_nonce( plugin_basename(__FILE__) ) . \'" />\';

    echo \'<textarea style="width: 100%;" rows="5" name="description" id="description">\'.get_post_meta($post->ID, \'description\', true).\'</textarea>\';
}

function dish_price_meta_box(){
    global $post;
    echo \'$<input type="text" name="dish_price" id="dish_price" value="\'.get_post_meta($post->ID, \'dish_price\', true).\'">\';
}

function dish_category_metabox()
{
    global $post;
    $out = \'<select name="parent_id">\'.PHP_EOL;
    $args = array(
        \'post_type\' => \'category\',
        \'order\' => \'ASC\'
    );
    $the_query = new WP_Query($args);
    if ($the_query->have_posts()) {
        while ($the_query->have_posts()) {
            $the_query->the_post();
            $out .= \'<option value="\'.get_the_ID().\'"\';
            if(get_post_meta($post->ID, \'parent_id\', true) == get_the_ID()){
                $out .= \' SELECTED\';
            }
            $out .= \'>\'.get_the_title().\'</option>\'.PHP_EOL;
        }
    }
    $out .= \'</select>\'.PHP_EOL;
    echo $out;
}
add_action(\'add_meta_boxes\', \'meta_boxes\');

1 个回复
最合适的回答,由SO网友:Milo 整理而成

$the_query->the_post(); 正在填充全局$post 每个帖子都在循环中。在前端查询中,您通常会调用wp_reset_postdata() 在运行辅助查询循环之后,但这在管理端不起作用。

使用get_posts 而不是WP_Query, 并使用foreach 回路-

$my_posts = get_posts( $args );
if( !empty( $my_posts ) ){
    foreach( $my_posts as $my_post ){
        echo $my_post->ID;
        echo get_the_title( $my_post );
    }
}

结束

相关推荐

仪表板:10个最后草稿页面和10个最后待审页面(Metabox)

我想在仪表板上显示一些用户角色(管理员、编辑器和auther)。包含10个最后草稿页和10个最后编辑页的框。对于最后编辑的10页,我使用以下自定义:add_action( \'wp_dashboard_setup\', \'admin_dashboard_last_edits_register\' ); function admin_dashboard_last_edits_register() { wp_add_dashboard_widget( __FUNCT