0

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.

  • I'll guess that it doesn't pass the second if condition in your pre_get_posts action, because count( $query->query ) is greater than 2. If you want to debug this, inspect the contents of $query and see what query vars are set and adjust your condition to capture those requests. – Milo Dec 22 '18 at 5:01
  • how can I debug query? – emir Dec 22 '18 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.