url rewriting - CPTUI rewrite disable when no post in CPTUI has been found

I have a CPTUI with the following data:function cptui_register_my_cpts_activaties() {*** Post Type: Activaties.*$label

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
Add a comment  | 

1 Answer 1

Reset to default 0

I 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' );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745453726a4628388.html

相关推荐

  • url rewriting - CPTUI rewrite disable when no post in CPTUI has been found

    I have a CPTUI with the following data:function cptui_register_my_cpts_activaties() {*** Post Type: Activaties.*$label

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信