admin管理员组文章数量:1425740
I have a CPTUI with the following data:
function cptui_register_my_cpts_activaties() {
/**
* Post Type: Activaties.
*/
$labels = array(
"name" => __( "Activaties", "red-button" ),
"singular_name" => __( "Activatie", "red-button" ),
);
$args = array(
"label" => __( "Activaties", "red-button" ),
"labels" => $labels,
"description" => "",
"public" => false,
"publicly_queryable" => true,
"show_ui" => true,
"delete_with_user" => false,
"show_in_rest" => true,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => false,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => true,
"rewrite" => array( "slug" => "activaties", "with_front" => false ),
"query_var" => true,
"supports" => array( "title" ),
);
register_post_type( "activaties", $args );
}
add_action( 'init', 'cptui_register_my_cpts_activaties' );
Now I have created a page as well, which is a subpage of another page. That looks likes this:
- Activaties
-- Aanleveren
So the url for Aanleveren
becomes /activaties/aanleveren
, which is the exact same of the CPTUI.
What I want:
When the post is NOT found as a CPTUI, I want to query the post_type
page.
Is this possible? If so, how can I achieve this?
I have a CPTUI with the following data:
function cptui_register_my_cpts_activaties() {
/**
* Post Type: Activaties.
*/
$labels = array(
"name" => __( "Activaties", "red-button" ),
"singular_name" => __( "Activatie", "red-button" ),
);
$args = array(
"label" => __( "Activaties", "red-button" ),
"labels" => $labels,
"description" => "",
"public" => false,
"publicly_queryable" => true,
"show_ui" => true,
"delete_with_user" => false,
"show_in_rest" => true,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => false,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => true,
"rewrite" => array( "slug" => "activaties", "with_front" => false ),
"query_var" => true,
"supports" => array( "title" ),
);
register_post_type( "activaties", $args );
}
add_action( 'init', 'cptui_register_my_cpts_activaties' );
Now I have created a page as well, which is a subpage of another page. That looks likes this:
- Activaties
-- Aanleveren
So the url for Aanleveren
becomes /activaties/aanleveren
, which is the exact same of the CPTUI.
What I want:
When the post is NOT found as a CPTUI, I want to query the post_type
page.
Is this possible? If so, how can I achieve this?
Share Improve this question asked May 29, 2019 at 12:43 TheatreOfSoulsTheatreOfSouls 101 2- CPT UI is a plugin, do you just mean CPT (Custom Post Type)? – Jacob Peattie Commented May 30, 2019 at 7:51
- Sorry, yes I meant CPT indeed – TheatreOfSouls Commented Jun 4, 2019 at 8:14
1 Answer
Reset to default 0I am guessing that you are searching for post and if post not found then you want to display page.
If you are using WordPress default search then you can try this code
function my_cptui_add_post_type_to_search( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( $query->is_search() ) {
$query->set(
'post_type',
array( 'post', 'page' )
);
}
}
add_filter( 'pre_get_posts', 'my_cptui_add_post_type_to_search' );
本文标签: url rewritingCPTUI rewrite disable when no post in CPTUI has been found
版权声明:本文标题:url rewriting - CPTUI rewrite disable when no post in CPTUI has been found 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745453725a2659004.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论