最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

customization - Getting 404 on child page with pre_get_posts() on custom posts

matteradmin10PV0评论

I want to have custom taxonomy for publishing different type of posts. I have registered several custom posts like with the code below

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

}

This code allows me to create Case Studies taxonomy and to access them directly via its slug (for example /my_new_case_study/), without the prefix (not like /case-studies/my_new_case_study/). To be able to access those posts in Case Studies I am using this function

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

Now when I create new regular post or page called "my_new_post" with the parent "my_old_post", my new slug is /my_old_post/my_new_post/ displayed in the dashboard, but when I click on it I get 404 message. If I remove "pre_get_posts" then I can access that page, but I can't access case studies posts. How can I access both hierarchy pages and new custom post "casestudies"?

I have flushed permalinks a number of times and tested everything.

Post a comment

comment list (0)

  1. No comments so far