Home › Forums › General Issues › How can i set custom fields in custom posts ?
I make custom posts by this codes.
function codex_custom_init() {
/**
* reportcat
*/
$reportcat_labels = array(
'name' => "レポートカテゴリ",
'singular_name' => "レポートカテゴリ",
'search_items' => "カテゴリを検索",
'all_items' => "すべてのカテゴリ",
'parent_item' => "親カテゴリ",
'parent_item_colon' => "親:",
'edit_item' => "カテゴリを編集",
'update_item' => "カテゴリを更新",
'add_new_item' => "新規カテゴリ",
'new_item_name' => "新規カテゴリ名",
'menu_name' => "レポートカテゴリ",
);
register_taxonomy( 'reportcat', array( 'report' ), array(
'hierarchical' => true,
'labels' => $reportcat_labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capabilities' => array( 'assign_terms' => 'edit_post_reports' )
));
/**
* customposts report
*/
$report_labels = array(
'name' => "レポート",
'singular_name' => "レポート",
'add_new' => "新規追加",
'add_new_item' => "新規レポート追加",
'edit_item' => "レポートを編集",
'new_item' => "新規レポート",
'view_item' => "表示",
'search_items' => "レポートを検索",
'not_found' => "見つかりません",
'not_found_in_trash' =>"ゴミ箱にはありません",
'parent_item_colon' => '親',
'menu_name' => 'レポート'
);
$report_args = array(
'labels' => $report_labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post_report', // 通常はpostだが任意のpost_reportにする
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 5,
'map_meta_cap' => true,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
'taxonomies' => array( 'reportcat' )
);
register_post_type( 'report', $report_args );
/**
* post_report にかかわる権限の付与
*/
// 新しい権限グループ「report_author」の作成
add_role( 'report_author', 'レポート投稿者' , array( 'read' => true ) );
// 権限グループをすべて取得
$roles = new WP_Roles();
// 付与すべき権限をすべて取得
$post_report_caps = $GLOBALS['wp_post_types']['report']->cap;
foreach ($roles->roles as $key => $value ) {
if ( $key == "subscriber" ) continue; // 購読者はスキップ
$role = get_role($key);
if ( $key == "report_author" ) { // レポート投稿者の時
$role->add_cap( "upload_files" ); // ファイルのアップロード権限付与
}
foreach ($post_report_caps as $post_report_cap) {
if ( $post_report_cap == "read" ) continue; // read は既に登録されているのでスキップ
if ( $key == "contributor" ) { // 投稿者に必要のない権限はスキップ
if ( $post_report_cap == "delete_others_post_reports" ) continue;
if ( $post_report_cap == "delete_private_post_reports" ) continue;
if ( $post_report_cap == "edit_others_post_reports" ) continue;
if ( $post_report_cap == "edit_private_post_reports" ) continue;
} elseif ( $key == "author" ) { // 寄稿者に必要のない権限はスキップ
if ( $post_report_cap == "delete_others_post_reports" ) continue;
if ( $post_report_cap == "delete_private_post_reports" ) continue;
if ( $post_report_cap == "delete_published_post_reports" ) continue;
if ( $post_report_cap == "edit_others_post_reports" ) continue;
if ( $post_report_cap == "edit_private_post_reports" ) continue;
if ( $post_report_cap == "edit_published_post_reports" ) continue;
if ( $post_report_cap == "publish_post_reports" ) continue;
} elseif ( $key == "report_author" ) { // レポート投稿者に必要のない権限はスキップ
if ( $post_report_cap == "delete_others_post_reports" ) continue;
if ( $post_report_cap == "delete_private_post_reports" ) continue;
if ( $post_report_cap == "edit_others_post_reports" ) continue;
if ( $post_report_cap == "edit_private_post_reports" ) continue;
}
$role->add_cap( $post_report_cap );
}
}
}
add_action( 'init', 'codex_custom_init', 0);
/**
* report_author のログイン後のリダイレクト先を変更
*/
function my_login_redirect( $redirect_to, $request, $user ) {
// is there a user to check?
if( !empty( $user->roles ) ) {
// check for admins
if( in_array( "report_author", $user->roles ) ) {
// redirect them to the default place
return home_url("/wp-admin/index.php");
} else {
return home_url("/wp-admin/");
}
}
}
add_filter("login_redirect", "my_login_redirect", 10, 3);
If change capability_type ‘post_report’ to ‘post’, advanced custom fields working well. But user can access /post-new.php?post_type=post. I want to user can access post-new.php?post_type=report only.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Plugin boilerplates can do some of the heavy lifting during initial development. We look at four options to speed up your plugin creation. https://t.co/ZtMsdBxAHw
— Advanced Custom Fields (@wp_acf) June 5, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.