在"自定义帖子类型"之间共享自定义分类;和"用户"

时间:2017-05-18 06:22:26

标签: wordpress

我想要一个名为技能的自定义分类 所以,我想在项目自定义帖子类型和用户之间分配这个分类 我的意思是,我可以在项目自定义帖子类型中选择技能分类(没问题
我可以在用户档案中选择技能分类(主要问题
任何人都可以帮助我吗? 感谢

1 个答案:

答案 0 :(得分:0)

显然,'tax_query'中还没有WP_User_Query的核心实现。

然而,使用get_objects_in_term

还有另一种方法
$taxonomy = 'skills';
$users = get_objects_in_term( $term_id, $taxonomy );    

if(!empty($users)){

    // WP_User_Query arguments
    $args = array (
        'role'           => 'teacher',
        'order'          => 'DESC',
        'orderby'        => 'user_registered',
        'include'        => $users
    );

    // The User Query
    $user_query = new WP_User_Query( $args );

    // The User Loop
    if ( ! empty( $user_query->results ) ) {
        foreach ( $user_query->results as $user ) {
            echo '<li><span>' . esc_html( $user->skill ) . '</span></li>';
        }
    } 
    else {
        // number of found 
    }
}
else {
    // no users found
}

或者如果您想为部分和用户添加分类,请使用以下代码。

根据您的要求和用户传递帖子类型

function my_register_user_taxonomy(){

 register_taxonomy(
    'profession',
    'user',
    array(
        'public' => true,
        'labels' => array(
            'name' => __( 'Professions' ),
            'singular_name' => __( 'Profession' ),
            'menu_name' => __( 'Professions' ),
            'search_items' => __( 'Search Professions' ),
            'popular_items' => __( 'Popular Professions' ),
            'all_items' => __( 'All Professions' ),
            'edit_item' => __( 'Edit Profession' ),
            'update_item' => __( 'Update Profession' ),
            'add_new_item' => __( 'Add New Profession' ),
            'new_item_name' => __( 'New Profession Name' ),
            'separate_items_with_commas' => __( 'Separate professions with commas' ),
            'add_or_remove_items' => __( 'Add or remove professions' ),
            'choose_from_most_used' => __( 'Choose from the most popular professions' ),
        ),
        'rewrite' => array(
            'with_front' => true,
            'slug' => 'author/profession' // Use 'author' (default WP user slug).
        ),
        'capabilities' => array(
            'manage_terms' => 'edit_users', // Using 'edit_users' cap to keep this simple.
            'edit_terms'   => 'edit_users',
            'delete_terms' => 'edit_users',
            'assign_terms' => 'read',
        ),
        'update_count_callback' => 'my_update_profession_count' // Use a custom function to update the count.
    )
);   

}