在自定义帖子上使用pre_get_posts()获取404

时间:2018-12-22 01:39:11

标签: wordpress custom-wordpress-pages

我想使用自定义分类法来发布不同类型的帖子。我已经用下面的代码注册了一些自定义帖子

function wtp_case_studies() {

    $labels = array(
        'name'                  => _x( 'Case Studies', 'Post Type General Name', 'text_domain' ),
        'singular_name'         => _x( 'Case Studies', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'             => __( 'Case Studies', 'text_domain' ),
        'name_admin_bar'        => __( 'Edit Team', 'text_domain' ),
        'archives'              => __( 'Аrchive', 'text_domain' ),
        'parent_item_colon'     => __( 'Parent Item:', 'text_domain' ),
        'all_items'             => __( 'All Case Studies', 'text_domain' ),
        'add_new_item'          => __( 'Add New Case Study', 'text_domain' ),
        'add_new'               => __( 'Add New Case Study', 'text_domain' ),
        'new_item'              => __( 'New Case Study', 'text_domain' ),
        'edit_item'             => __( 'Edit Case Study', 'text_domain' ),
        'update_item'           => __( 'Update Case Study', 'text_domain' ),
        'view_item'             => __( 'View Case Study', 'text_domain' ),
        'search_items'          => __( 'Search', 'text_domain' ),
        'not_found'             => __( 'Not Found', 'text_domain' ),
        'not_found_in_trash'    => __( 'Not Found', 'text_domain' ),
        'featured_image'        => __( 'Featured Image', 'text_domain' ),
        'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
        'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
        'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
        'insert_into_item'      => __( 'Insert into item', 'text_domain' ),
        'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
        'items_list'            => __( 'List of Cards', 'text_domain' ),
        'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
        'filter_items_list'     => __( 'Filter items list', 'text_domain' ),
    );
    $args = array(
        'label'                 => __( 'Management Profiles', 'text_domain' ),
        'description'           => __( 'anagement Profiles', 'text_domain' ),
        'labels'                => $labels,
        'supports'              => array( 'title','thumbnail',  'page-attributes'),
        'hierarchical'          => true,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'menu_icon'             => 'dashicons-megaphone',
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,        
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'page',
        //'rewrite'                 => array('slug' => 'case-studies')
        'rewrite'               => array('slug' => '/','with_front' => false,'pages'=> true,'feeds'=>true)
    );
    register_post_type( 'casestudies', $args );

}

此代码允许我创建案例研究分类法,并通过其子句(例如/ my_new_case_study /)直接访问它们,而没有前缀(不像/ case-studies / my_new_case_study /)。为了能够访问案例研究中的帖子,我正在使用此功能

function gp_add_cpt_post_names_to_main_query( $query ) {
    if ( ! $query->is_main_query() ) {
        return;
    }
    if ( ! isset( $query->query['page'] ) || 2 !== count( $query->query ) ) {
        return;
    }
    if ( empty( $query->query['name'] ) ) {
        return;
    }
    $query->set( 'post_type', array( 'post', 'page','casestudies') );
}
add_action( 'pre_get_posts', 'gp_add_cpt_post_names_to_main_query' );

现在,当我用父级“ my_old_post”创建一个名为“ my_new_post”的常规常规帖子或页面时,我的新信息将显示在/ my_old_post / my_new_post /中,但在单击时会收到404消息。如果删除“ pre_get_posts”,则可以访问该页面,但不能访问案例研究帖子。如何访问层次结构页面和新的自定义帖子“案例研究”?

我多次刷新了永久链接并测试了所有内容。

0 个答案:

没有答案