Force a user's posts category

时间:2017-01-05 作者:Adrien de Bosset

我不知道该怎么做。让我解释一下我的问题。

我有一个管理员,他可以定义很多类别。然后我希望他能够通过仪表板为每个用户指定多个类别。

然后,当用户发布帖子时(自定义帖子类型:news和others,使用相同的分类法“类别”),它将采用管理员在其profile中定义的帖子。

例如,管理员转到仪表板上的user\\u 1。他现在可以看到类别列表(当你添加帖子时),他选择该用户所属的类别。我们去:-餐厅-意大利

当用户\\u 1登录并立即添加新闻时,他将无法选择类别,但在发布新闻时,会将其添加到“餐厅”和“意大利”这两个类别中

任何人都有我已经尝试了三天的解决方案:(

提前感谢您的帮助

当做

阿德里安

对不起,我的英语不好,我正在尽力。

这是我在函数中的自定义帖子

// Post type
add_action(\'init\', \'postType\');
// Custom Post type
function postType()
{
// News
register_post_type(\'news\', array(
    \'label\' => \'News\',
    \'labels\' => array(
        \'name\' => \'News\',
        \'singular_name\' => \'News\',
        \'all_items\' => \'Toutes les News\',
        \'add_new_item\' => \'Ajouter une News\',
        \'edit_item\' => \'Éditer la News\',
        \'new_item\' => \'Nouvelle News\',
        \'view_item\' => \'Voir la News\',
        \'search_items\' => \'Rechercher parmi les News\',
        \'not_found\' => \'Pas de News\',
        \'not_found_in_trash\' => \'Pas de News dans la corbeille\'
    ),
    \'public\' => true,
    \'capability_type\' => \'post\',
    \'supports\' => array(
        \'title\',
        \'editor\',
        \'excerpt\',
        \'thumbnail\',
        \'author\'
    ),
    \'has_archive\' => true, 
    \'menu_icon\' => \'dashicons-welcome-write-blog\',
    \'hierarchical\' => true,
    \'taxonomies\' => array(\'category\'),
    \'capabilities\' => array(
        \'publish_posts\' => \'publish_news\',
        \'edit_posts\' => \'edit_news\',
        \'edit_others_posts\' => \'edit_others_news\',
        \'delete_posts\' => \'delete_news\',
        \'delete_others_posts\' => \'delete_others_news\',
        \'read_private_posts\' => \'read_private_news\',
        \'edit_post\' => \'edit_news\',
        \'delete_post\' => \'delete_news\',
        \'read_post\' => \'read_news\'
    ),

));

// News
register_post_type(\'etablissement\', array(
    \'label\' => \'Etablissement\',
    \'labels\' => array(
        \'name\' => \'Etablissement\',
        \'singular_name\' => \'Etablissements\',
        \'all_items\' => \'Tous les Etablissements\',
        \'add_new_item\' => \'Ajouter un Etablissement\',
        \'edit_item\' => \'Éditer l\\\'Etablissement\',
        \'new_item\' => \'Nouvel Etablissement\',
        \'view_item\' => \'Voir l\\\'Etablissement\',
        \'search_items\' => \'Rechercher parmi les Etablissements\',
        \'not_found\' => \'Pas d\\\'Etablissement\',
        \'not_found_in_trash\' => \'Pas d\\\'Etablissement dans la corbeille\'
    ),
    \'public\' => true,
    \'capability_type\' => \'post\',
    \'supports\' => array(
        \'title\',
        \'editor\',
        \'excerpt\',
        \'thumbnail\',
        \'author\'
    ),
    \'show_ui\' => true,
    \'has_archive\' => true, 
    \'menu_icon\' => \'dashicons-admin-multisite\',
    \'hierarchical\' => true,
    \'taxonomies\' => array(\'category\'),
    \'capabilities\' => array(
        \'publish_posts\' => \'publish_letablissements\',
        \'edit_posts\' => \'edit_letablissements\',
        \'edit_others_posts\' => \'edit_others_letablissements\',
        \'delete_posts\' => \'delete_letablissements\',
        \'delete_others_posts\' => \'delete_others_letablissements\',
        \'read_private_posts\' => \'read_private_letablissements\',
        \'edit_post\' => \'edit_letablissements\',
        \'delete_post\' => \'delete_letablissements\',
        \'read_post\' => \'read_letablissements\',
       // \'create_posts\' => false,
       \'create_posts\' => false,
    ),


));





// News
register_post_type(\'montreux\', array(
    \'label\' => \'Montreux\',
    \'labels\' => array(
        \'name\' => \'Montreux\',
        \'singular_name\' => \'Montreux\',
        \'all_items\' => \'Touts les articles Montreux\',
        \'add_new_item\' => \'Ajouter un article Montreux\',
        \'edit_item\' => \'Éditer l\\\'article Montreux\',
        \'new_item\' => \'Nouvel article Montreux\',
        \'view_item\' => \'Voir l\\\'article Montreux\',
        \'search_items\' => \'Rechercher parmi les articles Montreux\',
        \'not_found\' => \'Pas d\\\'article Montreux\',
        \'not_found_in_trash\' => \'Pas d\\\'article Montreux dans la corbeille\'
    ),
    \'public\' => true,
    \'capability_type\' => \'post\',
    \'supports\' => array(
        \'title\',
        \'editor\',
        \'excerpt\',
        \'thumbnail\',
        \'author\'
    ),
    \'has_archive\' => true, 
    \'menu_icon\' => \'dashicons-admin-home\',
    \'hierarchical\' => true,
    \'taxonomies\' => array(\'category\'),
    \'capabilities\' => array(
    \'edit_post\' => \'edit_montreux\',
    \'edit_posts\' => \'edit_montreux\',
    \'edit_others_posts\' => \'edit_other_montreux\',
    \'publish_posts\' => \'publish_montreux\',
    \'read_post\' => \'read_montreux\',
    \'read_private_posts\' => \'read_private_montreux\',
    \'delete_post\' => \'delete_montreux\',
    \'delete_published_posts\' => \'delete_published_montreux\'
),

));


 flush_rewrite_rules();

 }
谢谢

2 个回复
最合适的回答,由SO网友:Adrien de Bosset 整理而成

好的,谢谢你的帮助,我成功了:

1:当管理员进入用户选项页面时,他可以选择他所属的类别2:然后这个用户正在创建一条新闻,我们可以看到我正在隐藏类别框3:如果我们选中,我们可以看到新闻的类别设置来自用户选择的类别

12

所以,让我给出我的代码,也许它会帮助一些人:)

    function restrict_user_form_enqueue_scripts($hook) {
    if ( ! in_array($hook, array(\'profile.php\', \'user-edit.php\' )))
        return;
    wp_enqueue_script(\'jquery\');
    wp_enqueue_script( \'jquery.multiple.select\', get_template_directory_uri() . \'/js/jquery.multiple.select.js\' );
    wp_register_style( \'jquery.multiple.select_css\', get_template_directory_uri() . \'/css/multiple-select.css\', false, \'1.0.0\' );
    wp_enqueue_style( \'jquery.multiple.select_css\' );
}


add_filter(\'pre_option_default_category\', \'jam_change_default_category\');

function jam_change_default_category($ID) {
    // Avoid error or heavy load !
    if ( ! is_user_logged_in() )
        return $ID;
    $user_id = get_current_user_id();
    $restrict_cat = get_user_meta( $user_id, \'_access\', true);
    if ( is_array($restrict_cat) ) {
        return reset($restrict_cat);
    } else {
        return $ID;
    }
}

/**
* Exclude categories which arent selected for this user.
*/
add_filter( \'get_terms_args\', \'restrict_user_get_terms_args\', 10, 2 );

function restrict_user_get_terms_args( $args, $taxonomies ) {
    // Dont worry if we\'re not in the admin screen
    if (! is_admin() || $taxonomies[0] !== \'category\')
        return $args;
    // Admin users are exempt.
    $currentUser = wp_get_current_user();
    if (in_array(\'administrator\', $currentUser->roles))
        return $args;

    $include = get_user_meta( $currentUser->ID, \'_access\', true);

    $args[\'include\'] = $include;
    return $args;
    //var_dump($include);
}
// Display and save data in admin dashboard
function restrict_user_form( $user ) {
    // A little security
    if ( ! current_user_can(\'add_users\'))
        return false;
    $args = array(
        \'show_option_all\'    => \'\',
        \'orderby\'            => \'ID\',
        \'order\'              => \'ASC\',
        \'show_count\'         => 0,
        \'hide_empty\'         => 0,
        \'child_of\'           => 0,
        \'exclude\'            => \'\',
        \'echo\'               => 0,
        \'hierarchical\'       => 1,
        \'name\'               => \'allow\',
        \'id\'                 => \'\',
        \'class\'              => \'postform\',
        \'depth\'              => 0,
        \'tab_index\'          => 0,
        \'taxonomy\'           => \'category\',
        \'hide_if_empty\'      => false,
        \'walker\'             => \'\'
    );

    $dropdown = wp_dropdown_categories($args);
    // We are going to modify the dropdown a little bit.
    $dom = new DOMDocument();
    //$dom->loadHTML($dropdown, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
    $dom->loadHTML( mb_convert_encoding($dropdown, \'HTML-ENTITIES\', \'UTF-8\') );
    $xpath = new DOMXpath($dom);
    $selectPath = $xpath->query("//select[@id=\'allow\']");

    if ($selectPath != false) {
        // Change the name to an array.
        $selectPath->item(0)->setAttribute(\'name\', \'allow[]\');
        // Allow multi select.
        $selectPath->item(0)->setAttribute(\'multiple\', \'yes\');

        $selected = get_user_meta( $user->ID, \'_access\', true);
        // Flag as selected the categories we\'ve previously chosen
        // Do not throught error in user\'s screen ! // @JamViet
        if ( $selected )
        foreach ($selected as $term_id) {
            // fixed delete category make error !
            if (!empty($term_id) && get_the_category_by_ID($term_id) ){
                $option = $xpath->query("//select[@id=\'allow\']//option[@value=\'$term_id\']");
                $option->item(0)->setAttribute(\'selected\', \'selected\');
            }
        }
    }
?>
    <h3><?php _e(\'Catégories de cet utilisateur\', \'restrict-author-posting\'); ?></h3>
    <table class="form-table">
        <tr>
            <th><label for="access"><?php _e(\'Choisissez les categories\', \'restrict-author-posting\') ?>:</label></th>
            <td>
                <?php echo $dom->saveXML($dom);?>
                <span class="description"><?php _e(\'\', \'restrict-author-posting\') ?></span>
            </td>
        </tr>

    </table>
    <table class="form-table">
        <tr>
            <th><label for="access"><?php _e(\'Voir seulement ces fichiers medias\', \'restrict-author-posting\') ?></label></th>
            <td>
                    <fieldset>
                    <legend class="screen-reader-text"><span><?php _e(\'Oui\', \'restrict-author-posting\') ?></span></legend>
                    <label for="_restrict_media">
                    <input type="checkbox" <?php checked (get_user_meta($user->ID, \'_restrict_media\', true), 1, 1 ) ?> value="1" id="_restrict_media" name="_restrict_media">
                <?php _e(\'Oui\', \'restrict-author-posting\') ?></label>
                    </fieldset>
            </td>
        </tr>
    </table>
    <script>
    <!--
        jQuery(\'select#allow\').multipleSelect();
    -->
    </script>
<?php
}


// Restrict Save Data
function restrict_save_data( $user_id ) {
    // check security
    if ( ! current_user_can( \'add_users\' ) )
        return false;
    // admin can not restrict himself
    if ( get_current_user_id() == $user_id )
        return false;
    // and last, save it
    if ( ! empty ($_POST[\'_restrict_media\']) ) {
        update_user_meta( $user_id, \'_restrict_media\', $_POST[\'_restrict_media\'] );
    } else {
        delete_user_meta( $user_id, \'_restrict_media\' );
    }
    if ( ! empty ($_POST[\'allow\']) ) {
        update_user_meta( $user_id, \'_access\', $_POST[\'allow\'] );
    } else  {
        delete_user_meta( $user_id, \'_access\' );
    }
}

// Remove meta box for non admin    
function remove_metaboxes() {
remove_meta_box( \'categorydiv\',\'news\',\'normal\' );
remove_meta_box( \'categorydiv\',\'etablissement\',\'normal\' );
}

// Save Category News
function save_category_news($post_ID, $post) {
    $currentUser = wp_get_current_user();
    $cat = get_user_meta( $currentUser->ID, \'_access\', true);
    $cat = array_map(\'intval\', $cat);
    wp_set_object_terms($post_ID, $cat, \'category\', true);

}

// Save Category Etablissement
function save_category_etablissement($post_ID, $post) {
    $currentUser = wp_get_current_user();
    $cat = get_user_meta( $currentUser->ID, \'_access\', true);
    $cat = array_map(\'intval\', $cat);
    wp_set_object_terms($post_ID, $cat, \'category\', true);

}
// Action let\'s go
add_action( \'admin_enqueue_scripts\', \'restrict_user_form_enqueue_scripts\' );
add_action( \'show_user_profile\', \'restrict_user_form\' );
add_action( \'edit_user_profile\', \'restrict_user_form\' );
add_action( \'personal_options_update\', \'restrict_save_data\' );
add_action( \'edit_user_profile_update\', \'restrict_save_data\' );
add_action( \'save_post_news\', \'save_category_news\', 10, 3 );
add_action( \'save_post_etablissement\', \'save_category_etablissement\', 10, 3 );
add_action( \'admin_menu\', \'remove_metaboxes\');

SO网友:CodeMascot

这是您的解决方案-

add_action( \'save_post_news\', \'the_dramatist_save_post_cat\', 10, 3 );

function the_dramatist_save_post_cat($post_ID, $post, $update) {
    $u_id = get_current_user_id();
    if ($u_id == 1) { // Here put your user ID.
        $cats = array( 108, 109 ); // Here put "restaurant" and "italian" category ID
        // setting the category
        wp_set_object_terms($post_ID, $cats, "category", TRUE);
    }
}
希望这有帮助。

相关推荐

Post in multiple categories

我尝试在多个类别中列出帖子。管理面板中一切正常。我进入所有帖子,选择所需的帖子,然后进入编辑并选择一个类别。保存更改后,在“类别”选项卡下会写入旧类别和新类别。现在,当我访问网站并选择更新类别时,只有旧帖子,没有新帖子。例如:我有类别:电影、游戏、最佳和帖子:最佳电影、最佳游戏、最佳,我需要这样:最佳电影属于电影类别最佳游戏属于游戏类别,两者都属于最佳类别我使用日期和职位名称作为永久链接。