用户选择内部定制Metabox

时间:2013-08-29 作者:Rizzo

我正在使用人造的自定义元框框架(https://github.com/humanmade/Custom-Meta-Boxes). 我想在我的metabox中添加一个select下拉列表,该下拉列表由我的WP站点上具有多个角色的用户填充。引用此帖子:Get multiple roles with get_users

我想到了:

add_filter( \'cmb_meta_boxes\', \'users_metabox\' );

function eusers_metabox( array $meta_boxes ) {

    $prefix = \'user_\';

    global $wpdb;
    $blog_id = get_current_blog_id();

    $user_query = new WP_User_Query( array(
        \'meta_query\' => array(
            \'relation\' => \'OR\',
            array(
                \'key\' => $wpdb->get_blog_prefix( $blog_id ) . \'capabilities\',
                \'value\' => \'Administrator\',
                \'compare\' => \'like\'
              ),
            array(
                \'key\' => $wpdb->get_blog_prefix( $blog_id ) . \'capabilities\',
                \'value\' => \'Editor\',
                \'compare\' => \'like\'
              )
          )
      ) 
    );

    $fields = array(
    array( 
            \'id\'   => $prefix . \'user_sub\', 
            \'name\' => \'Subscriber User\', 
            \'type\'     => \'select\',
            \'use_ajax\' => false,
            \'options\'  => $user_query,  // this is where you populate the select in metabox
        ),
    );

    $meta_boxes[] = array(
        \'title\' => \'Location Info\',
        \'pages\' => \'em_users\',
        \'context\'    => \'normal\',
        \'priority\'   => \'high\',
        \'fields\' => $fields
    );

    return $meta_boxes; 

}
有些效果,但似乎只返回大写字母。有什么想法吗?

需要将(UserID)输出为变量常量

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

我认为问题是您传递的是WP\\u User\\u查询对象,而不是查询结果。尝试更改:

$fields = array(
array( 
        \'id\'   => $prefix . \'user_sub\', 
        \'name\' => \'Subscriber User\', 
        \'type\'     => \'select\',
        \'use_ajax\' => false,
        \'options\'  => $user_query,  // this is where you populate the select in metabox
    ),
);
收件人:

$users_ids = array();
if ( !empty( $user_query->results )){
    foreach($user_query->results as $user){
        $users_ids[] = $user->ID;
    }
}

$fields = array(
array( 
        \'id\'   => $prefix . \'user_sub\', 
        \'name\' => \'Subscriber User\', 
        \'type\'     => \'select\',
        \'use_ajax\' => false,
        \'options\'  => $users_ids,  // this is where you populate the select in metabox
    ),
);

结束

相关推荐

自定义帖子类型默认设置为打开注释,不显示METABOX

我为特定帖子创建了一个新的自定义帖子类型。我的新自定义帖子类型中的帖子在默认情况下将注释设置为“关闭”。我需要在默认情况下将注释设置为“开”。在我的functions.php 文件我有:\'supports\' => array(\'editor\',\'comments\') 以及function default_comments_on( $data ) { if( $data[\'post_type\'] == \'registro\' && $data