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

本文标签: url rewritingCPTUI rewrite disable when no post in CPTUI has been found