未在CPTUI中找到开机自检时禁用CPTUI重写

时间:2019-05-29 作者:TheatreOfSouls

我有一个包含以下数据的CPTUI:

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\' );
现在我也创建了一个页面,它是另一个页面的子页面。看起来是这样的:

- Activaties
-- Aanleveren
所以的urlAanleveren 成为/activaties/aanleveren, 这与CPTUI完全相同。

What I want:当未找到作为CPTUI的帖子时,我想查询post_type 第页

这可能吗?如果是,我如何才能做到这一点?

1 个回复
SO网友:Parthavi Patel

我猜您正在搜索帖子,如果找不到帖子,那么您希望显示页面。

如果您正在使用WordPress default search 然后您可以尝试此代码

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

相关推荐