注册具有相同permalink结构的两个post类型时,会将它们添加到具有相同索引的重写规则数组中。如果要将两个索引相同的元素添加到php数组中,第一个元素将被覆盖。您的重写将只适用于一种帖子类型,即第二种。
要显示第一种帖子类型的帖子,您必须告诉WordPress要选择哪种帖子类型。要找到它,您必须从这两种职位类型中查询职位数据库,并检查当前职位类型具有哪种职位类型。
我希望下面的代码可以帮助您理解发生了什么。
/**
* Register event 1 post type
*
* Method is used by init hook
*/
function wpse_287785_register_event_1_post_type() {
$labels = array(
\'name\' => __( \'Events\' ),
\'singular_name\' => __( \'Event\' ),
\'add_new\' => __( \'Add new\' ),
\'add_new_item\' => __( \'Add new\' ),
\'edit_item\' => __( \'Edit\' ),
\'new_item\' => __( \'New\' ),
\'view_item\' => __( \'View\' ),
\'search_items\' => __( \'Search\' ),
\'not_found\' => __( \'Not found\' ),
\'not_found_in_trash\' => __( \'Not found Events in trash\' ),
\'parent_item_colon\' => __( \'Parent\' ),
\'menu_name\' => __( \'Events\' ),
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => false,
\'supports\' => array( \'title\', \'page-attributes\' ),
\'taxonomies\' => array(),
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'show_in_nav_menus\' => false,
\'publicly_queryable\' => true,
\'exclude_from_search\' => false,
\'has_archive\' => true,
\'query_var\' => true,
\'can_export\' => true,
\'rewrite\' => array(\'slug\' => \'event\'),
\'capability_type\' => \'post\',
);
register_post_type( \'event_1\', $args );
}
add_action( \'init\', \'wpse_287785_register_event_1_post_type\' );
/**
* Register event 2 post type
*
* Method is used by init hook
*/
function wpse_287785_register_event_2_post_type() {
$labels = array(
\'name\' => __( \'Events\' ),
\'singular_name\' => __( \'Event\' ),
\'add_new\' => __( \'Add new\' ),
\'add_new_item\' => __( \'Add new\' ),
\'edit_item\' => __( \'Edit\' ),
\'new_item\' => __( \'New\' ),
\'view_item\' => __( \'View\' ),
\'search_items\' => __( \'Search\' ),
\'not_found\' => __( \'Not found\' ),
\'not_found_in_trash\' => __( \'Not found Events in trash\' ),
\'parent_item_colon\' => __( \'Parent\' ),
\'menu_name\' => __( \'Events\' ),
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => false,
\'supports\' => array( \'title\', \'page-attributes\' ),
\'taxonomies\' => array(),
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'show_in_nav_menus\' => false,
\'publicly_queryable\' => true,
\'exclude_from_search\' => false,
\'has_archive\' => true,
\'query_var\' => true,
\'can_export\' => true,
\'rewrite\' => array(\'slug\' => \'event\'),
\'capability_type\' => \'post\',
);
register_post_type( \'event_2\', $args );
}
add_action( \'init\', \'wpse_287785_register_event_2_post_type\' );
/**
* Check if post exits as event_1 post type or as event_2 post type.
*/
function wpse_287785_parse_request($query_vars) {
if( !is_admin() ) { // Do not parse request on admin.
$slug = ( isset( $query_vars[\'name\'] ) && !empty( $query_vars[\'name\'] ) ) ? $query_vars[\'name\'] : false;
if($slug) {
// Check if post exits as event_1 or event_2 post type
$query = new WP_Query(array(
\'post_type\' => array(\'event_1\', \'event_2\'),
\'name\' => $slug,
));
if( $query->have_posts() ) {
$post_type = $query->post->post_type;
// Overwrite query vars for current post type
$query_vars = array(
\'page\' => \'\',
$post_type => $slug,
\'post_type\' => $post_type,
\'name\' => $slug,
);
return $query_vars;
}
}
}
return $query_vars;
}
add_filter( \'request\', \'wpse_287785_parse_request\' );