• down vote
    favorite
    I use WordPress 4.7 hosted on a W2012 server with MySQL 5.7.9 and PHP 7. I have a child theme, and in my child theme, I have created a custom post type with the code below. In WP settings, permalinks structure are set to “post name”. Permalinks work fine for pages and posts, but I have strange behaviors with my custom post type.

    I have created a custom post type with slug ‘accueil-parcours-107’ under parent custom post type with slug ‘place-01’.

    When I enter https://responsive.knowledgeplaces.com/accueil-parcours-107/ I am redirected to https://responsive.knowledgeplaces.com/places/place-01/accueil-parcours-107/

    I would like to get a 404 error, how can I do this?

    Here is the code for custom post type:

    
    register_post_type( 'kpcms_place',
    array(
        'labels' => array(
            'name' => __( 'Knowledge Places Pages', 'KPCMS' ),
            'singular_name' => __( 'Knowledge Place Page', 'KPCMS' ),
            'add_new_item' => __( 'Add a Knowledge Place Page', 'KPCMS' ),
            'edit_item' => __( 'Edit Knowledge Place Page', 'KPCMS' ),
            'new_item' => __( 'New Knowledge Place Page', 'KPCMS' ),
            'view_item' => __( 'View Knowledge Place Page', 'KPCMS' ),
            'search_items' => __( 'Search Knowledge Places Pages', 'KPCMS' ),
            'not_found' => __( 'No Knowledge Places Pages found', 'KPCMS' ),
            'not_found_in_trash' => __( 'No Knowledge Places Pages found in Trash', 'KPCMS' ),
            'parent_item_column' => __( 'Parent Knowledge Place Page:', 'KPCMS' ),
            'all_items' => __( 'All Knowledge Places Pages', 'KPCMS' ),
            'insert_into_item' => __( 'Insert into Knowledge Place Page', 'KPCMS' ),
            'uploaded_to_this_item' => __( 'Uploaded to this Knowledge Place Page', 'KPCMS' ),
            'menu_name' => __( 'Knowledge Places' )
        ),
        'query_var' => true,
        'public' => true,
        'hierarchical' => true,
        'exclude_from_search' => true,
        'publicly_queryable' => true,
        'menu_icon' => 'dashicons-location',
        'rewrite' => array(
            'slug' => 'places'
        ),
        'taxonomies' => array(
            'category',
            'post_tag'
            ),
        'capabilities' => array(
            'edit_post'          => 'edit_kpcms_place',
            'read_post'          => 'read_kpcms_place',
            'delete_post'        => 'delete_kpcms_place',
            'edit_posts'         => 'edit_kpcms_places',
            'edit_others_posts'  => 'edit_others_kpcms_places',
            'publish_posts'      => 'publish_kpcms_places',
            'read_private_posts' => 'read_private_kpcms_places',
            'create_posts'       => 'edit_kpcms_places'
        ),
        'supports' => array(
            'title',
            'editor',
            'author',
            'comments',
            'revisions',
            'page-attributes'
        ),
    )
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Use the “parse_query” action. Check if the queried post is the right type and has a non-zero parent. If so, check if there’s a slash in the pagename, indicating the parent was included in the request. If there’s no slash, call the set_404() method of the passed query object.

    Thread Starter Eric Malalel

    (@teachlynx)

    Thanks for feedback.
    I have added this code which uses ChromePHP to help me see what happens:

    
    add_action('parse_query', 'kpcms_parse_query');
    function kpcms_parse_query( $wp_query ) {
    	$post = get_post($wp_query->post);
    	if ( (get_post_type($post)=='kpcms_place') && ($post->post_parent !== '0') ) {
    		ChromePhp::log('post name: ' . $post->post_name);
    		ChromePhp::log('permalink: ' . get_permalink());
    	}
    }
    

    I get this in the console:
    begin parse query
    log.js:137 post name: accueil-parcours-106
    log.js:137 permalink: https://responsive.knowledgeplaces.com/places/place-01/accueil-parcours-106/
    log.js:137 begin parse query
    log.js:137 post name: accueil-parcours-106
    log.js:137 permalink: https://responsive.knowledgeplaces.com/places/place-01/accueil-parcours-106/

    Sorry, don’t understand where to go from there

    Moderator bcworkz

    (@bcworkz)

    Try just using print_r( $wp_query ); in your callback to see the data passed to you. Or if ChromePHP supports logging an entire multi-line array structure you can do that.

    You’ll see the post is already in the query data, no need to get the post again. You’ll also see a “pagename” value that includes the request sans domain and protocol.

    print_r() (without logging) will output the data on top of your page in a dense block, but if you view the HTML source you’ll see structured output.

    Thread Starter Eric Malalel

    (@teachlynx)

    Thanks.
    Meanwhile, I ended up using the get_page_by_path() function.
    It returns null when called with “accueil-parcours-107”
    and finds a post when called with “place-01/accueil-parcours-107”

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘issue with hierarchical custom post type’ is closed to new replies.