在我的插件中,我想我使用了很多标准和文档化的方法来添加我的自定义帖子类型和两个税(猫和标签),但我唯一要做的就是设置自定义capability_type
还有烫发。
以下是代码:
// =============================
// custom taxonomies (cats and tags)
// =============================
function custom_taxonomy() {
$labels = array(
\'name\' => _x( \'ifcc Categories\', \'Taxonomy General Name\', \'text_domain\' ),
\'singular_name\' => _x( \'ifcc Category\', \'Taxonomy Singular Name\', \'text_domain\' ),
\'menu_name\' => __( \'ifcc category\', \'text_domain\' ),
\'all_items\' => __( \'All Items\', \'text_domain\' ),
\'parent_item\' => __( \'Parent Item\', \'text_domain\' ),
\'parent_item_colon\' => __( \'Parent Item:\', \'text_domain\' ),
\'new_item_name\' => __( \'New Item Name\', \'text_domain\' ),
\'add_new_item\' => __( \'Add New Item\', \'text_domain\' ),
\'edit_item\' => __( \'Edit Item\', \'text_domain\' ),
\'update_item\' => __( \'Update Item\', \'text_domain\' ),
\'separate_items_with_commas\' => __( \'Separate items with commas\', \'text_domain\' ),
\'search_items\' => __( \'Search Items\', \'text_domain\' ),
\'add_or_remove_items\' => __( \'Add or remove items\', \'text_domain\' ),
\'choose_from_most_used\' => __( \'Choose from the most used items\', \'text_domain\' ),
\'not_found\' => __( \'Not Found\', \'text_domain\' ),
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => true,
\'public\' => true,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'show_in_nav_menus\' => true,
\'show_tagcloud\' => true,
\'rewrite\' => array( \'slug\' => \'ifcc-categories\' ),
\'query_var\' => true
);
register_taxonomy( \'ifcc_cats\', array( \'ifcc\' ), $args );
$labels = array(
\'name\' => _x( \'Tags\', \'Taxonomy General Name\', \'text_domain\' ),
\'singular_name\' => _x( \'Tag\', \'Taxonomy Singular Name\', \'text_domain\' ),
\'menu_name\' => __( \'Tag\', \'text_domain\' ),
\'all_items\' => __( \'All Items\', \'text_domain\' ),
\'parent_item\' => __( \'Parent Item\', \'text_domain\' ),
\'parent_item_colon\' => __( \'Parent Item:\', \'text_domain\' ),
\'new_item_name\' => __( \'New Item Name\', \'text_domain\' ),
\'add_new_item\' => __( \'Add New Item\', \'text_domain\' ),
\'edit_item\' => __( \'Edit Item\', \'text_domain\' ),
\'update_item\' => __( \'Update Item\', \'text_domain\' ),
\'separate_items_with_commas\' => __( \'Separate items with commas\', \'text_domain\' ),
\'search_items\' => __( \'Search Items\', \'text_domain\' ),
\'add_or_remove_items\' => __( \'Add or remove items\', \'text_domain\' ),
\'choose_from_most_used\' => __( \'Choose from the most used items\', \'text_domain\' ),
\'not_found\' => __( \'Not Found\', \'text_domain\' ),
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => false,
\'public\' => true,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'show_in_nav_menus\' => true,
\'show_tagcloud\' => true,
\'query_var\' => true
);
register_taxonomy( \'ifcc_tags\', array( \'ifcc\' ), $args );
}
add_action( \'init\', \'custom_taxonomy\', 20 );
// =============================
// register ifcc custom post type for new tax up
// =============================
function custom_post_type() {
global $woo_contest;
$labels = array(
\'name\' => _x( \'IFCC Submits\', \'Post Type General Name\', \'text_domain\' ),
\'singular_name\' => _x( \'IFCC Submit\', \'Post Type Singular Name\', \'text_domain\' ),
\'menu_name\' => __( \'IFCC Submit\', \'text_domain\' ),
\'parent_item_colon\' => __( \'Parent Item:\', \'text_domain\' ),
\'all_items\' => __( \'All Items\', \'text_domain\' ),
\'view_item\' => __( \'View Item\', \'text_domain\' ),
\'add_new_item\' => __( \'Add New Item\', \'text_domain\' ),
\'add_new\' => __( \'Add New\', \'text_domain\' ),
\'edit_item\' => __( \'Edit Item\', \'text_domain\' ),
\'update_item\' => __( \'Update Item\', \'text_domain\' ),
\'search_items\' => __( \'Search Item\', \'text_domain\' ),
\'not_found\' => __( \'Not found\', \'text_domain\' ),
\'not_found_in_trash\' => __( \'Not found in Trash\', \'text_domain\' ),
);
$args = array(
\'label\' => __( \'post_type\', \'text_domain\' ),
\'description\' => __( \'ifcc submit\', \'text_domain\' ),
\'labels\' => $labels,
\'supports\' => array( \'title\', \'editor\', \'revisions\', \'custom-fields\', \'post-formats\', \'author\', \'thumbnail\' ),
\'taxonomies\' => array( \'ifcc_cats\', \'ifcc_tags\' ),
\'hierarchical\' => false,
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'show_in_nav_menus\' => true,
\'show_in_admin_bar\' => true,
\'menu_position\' => 20,
\'can_export\' => true,
\'has_archive\' => true,
\'exclude_from_search\' => true,
\'publicly_queryable\' => true,
\'capability_type\' => \'ifcc\',
\'capabilities\' => array( \'read_ifcc\', \'edit_ifcc\', \'delete_ifcc\' ),
\'map_meta_cap\' => true
);
register_post_type( \'ifcc\', $args );
register_taxonomy_for_object_type( \'ifcc_cats\', \'ifcc\' );
register_taxonomy_for_object_type( \'ifcc_tags\', \'ifcc\' );
// flush_rewrite_rules();
}
add_action( \'init\', \'custom_post_type\', 30 );
然后我添加它来修复管理员和客户(该cpt的真实用户,但仅用于前端任务,上载图片并将表单提交到后端,保存帖子)
// =============================
// fix admin and woo user perms
// =============================
function ifccperms(){
$role = get_role(\'administrator\');
$role->add_cap( \'edit_ifcc\' );
$role->add_cap( \'read_ifcc\' );
$role->add_cap( \'delete_ifcc\' );
$role->add_cap( \'delete_ifccs\' );
$role->add_cap( \'edit_ifccs\' );
$role->add_cap( \'edit_others_ifccs\' );
$role->add_cap( \'publish_ifccs\' );
$role->add_cap( \'read_private_ifccs\' );
$role->add_cap( \'delete_private_ifccs\' );
$role->add_cap( \'delete_published_ifccs\' );
$role->add_cap( \'delete_others_ifccs\' );
$role->add_cap( \'edit_private_ifccs\' );
$role->add_cap( \'edit_published_ifccs\' );
if( null == add_role( \'customer\', \'customer\' ) ){
$role = get_role(\'customer\');
$role->add_cap( \'edit_ifcc\' );
$role->add_cap( \'edit_ifccs\' );
$role->add_cap( \'delete_ifcc\' );
$role->add_cap( \'delete_ifccs\' );
$role->add_cap( \'edit_published_ifccs\' );
$role->add_cap( \'publish_ifccs\' );
$role->add_cap( \'read_ifcc\' );
$role->add_cap( \'upload_files\' );
}
}
add_action( \'init\', \'ifccperms\', 40 );
// =============================
// dirty hack to alter sql query == do not know why does the query works wrongly?
// here I just alter one
// =============================
function checkq($a){
return str_replace(\'product\', \'ifcc\', $a );
}
add_filter( \'posts_request\', \'checkq\' );
一切都很好,在后端我可以看到cpt和tax,每个用户只能看到自己的帖子,从前端上传效果很好,等等。
但问题来了。当我尝试从前端访问分类页面时
root/ifcc-categories/
页面转到“未找到页面”,404错误。
我确实钩住检查了保存页面的sql,它回应了我:
SELECT wp_posts.*
FROM wp_posts
WHERE 1=1
AND wp_posts.post_name = \'ifcc-categories\'
AND wp_posts.post_type = \'post\'
ORDER BY wp_posts.post_date DESC
我对两件事感到困惑。为什么wordpress会这么认为
post_name
岗位类型为岗位?
还有一件事,如果我转到术语url,例如:
root/ifcc-categories/design/
我得到以下sql字符串:
SELECT ... WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (7) )
AND wp_posts.post_type
IN (\'post\', \'page\', \'attachment\', \'product\')
AND (wp_posts.post_status = \'publish\'
OR wp_posts.post_author = 1
AND wp_posts.post_status = \'private\')
如果有人对这里发生的事情有任何线索,我很高兴听到,因为我迷路了