0

I've tried all the solutions I can find on this and I'm still stuck. This post has a good thorough approach to debugging, but it hasn't helped.

Part of the issue may be because I'm trying to get the Flatpack theme to change its behaviour. We're locked into this theme now. I'm doing this to make the portfolio and services CPTs hierarchical:

add_action( 'init', 'lf_modify_post_types', 999999 );
function lf_modify_post_types() {

if ( post_type_exists( 'portfolio' ) ) {

    $args = get_post_type_object( 'portfolio' );
    $args->rewrite['slug'] = 'portfolio';
    $args->rewrite['with_front'] = false;
    $args->hierarchical = true;
    $args->supports[] = 'page-attributes';
            $args->query_var = true;
    register_post_type( 'portfolio', $args );

}

if ( post_type_exists( 'services' ) ) {

    $args = get_post_type_object( 'services' );
    $args->rewrite['slug'] = 'services';
    $args->rewrite['with_front'] = false;
    $args->hierarchical = true;
    $args->supports[] = 'page-attributes';
            $args->query_var = true;
    register_post_type( 'services', $args );

}

}

As far as I can see, this works to make sure all the relevant CPT settings are right (including setting query_var to true, which seems to be a common "solution" to this issue). A dump of the portfolio CPT object at the end of the above gives this:

stdClass Object (
[labels] => stdClass Object
    (
        [name] => Portfolio
        [singular_name] => Portfolio
        [add_new] => Add Portfolio Item
        [add_new_item] => Add New Portfolio Item
        [edit_item] => Edit 
        [new_item] => New Portfolio Item
        [view_item] => View Portfolio Item
        [search_items] => Search Portfolio Items
        [not_found] => No portfolio items found
        [not_found_in_trash] => No portfolio items found in Trash
        [parent_item_colon] => 
        [all_items] => Portfolio
        [menu_name] => Portfolio
        [slug] => portfolio
        [name_admin_bar] => Portfolio
    )

[description] => 
[public] => 1
[hierarchical] => 1
[exclude_from_search] => 
[publicly_queryable] => 1
[show_ui] => 1
[show_in_menu] => 1
[show_in_nav_menus] => 1
[show_in_admin_bar] => 1
[menu_position] => 
[menu_icon] => http://languagefutures.localhost/wp-content/themes/language-futures/ocmx/images/icons/portfolio-icon.png
[capability_type] => post
[map_meta_cap] => 1
[register_meta_box_cb] => 
[taxonomies] => Array
    (
    )

[has_archive] => 
[rewrite] => Array
    (
        [slug] => portfolio
        [with_front] => 
        [pages] => 1
        [feeds] => 
        [ep_mask] => 1
    )

[query_var] => portfolio
[can_export] => 1
[delete_with_user] => 
[_builtin] => 
[_edit_link] => post.php?post=%d
[name] => portfolio
[cap] => stdClass Object
    (
        [edit_post] => edit_post
        [read_post] => read_post
        [delete_post] => delete_post
        [edit_posts] => edit_posts
        [edit_others_posts] => edit_others_posts
        [publish_posts] => publish_posts
        [read_private_posts] => read_private_posts
        [read] => read
        [delete_posts] => delete_posts
        [delete_private_posts] => delete_private_posts
        [delete_published_posts] => delete_published_posts
        [delete_others_posts] => delete_others_posts
        [edit_private_posts] => edit_private_posts
        [edit_published_posts] => edit_published_posts
        [create_posts] => edit_posts
    )

[label] => Portfolio
)

Flushed rules, and still:

http://languagefutures.localhost/portfolio/project-and-themes/ (works OK) http://languagefutures.localhost/portfolio/project-and-themes/who-do-you-think-you-are/ (404)

I've used the Rewrite Rules Inspector plugin on the latter URL, and it shows:

portfolio/(.+?)(/[0-9]+)?/?$    index.php?portfolio=$matches[1]&page=$matches[2]    portfolio
(.?.+?)(/[0-9]+)?/?$            index.php?pagename=$matches[1]&page=$matches[2] page

AFAIK all this is as it should be. Using the Query Monitor plugin, these are the queries being run that seem to be relevant:

SELECT ID, post_name, post_parent, post_type
FROM k3bex_posts
WHERE post_name IN ('project-and-themes','who-do-you-think-you-are')
AND (post_type = 'portfolio'
OR post_type = 'attachment')

Caller:
get_page_by_path()-
WP_Query->get_posts()
WP_Query->query()
WP->query_posts()
WP->main()
wp()

Returns: 2

SELECT k3bex_posts.*
FROM k3bex_posts 
WHERE 1=1 
AND (k3bex_posts.ID = '1493')
AND k3bex_posts.post_parent = 0 
AND k3bex_posts.post_type = 'portfolio' 
ORDER BY k3bex_posts.post_date DESC

Caller:
WP_Query->get_posts()-
WP_Query->query()
WP->query_posts()
WP->main()
wp()

Returns: 0

SELECT ID
FROM k3bex_posts
WHERE post_name LIKE 'who-do-you-think-you-are%'
AND post_type = 'portfolio'
AND post_status = 'publish'

Caller:
redirect_guess_404_permalink()-
redirect_canonical()
do_action('template_redirect')

Returns: 1

The obvious stand-out is the second one. 1493 is indeed the right ID for the child page. Why is the query specifying post_parent = 0? I'm continuing to look into it but if anyone has experienced anything like this and survived, tips would be great.

0

Turned out to be interference from another Flatpack hack I had in place to stop it displaying child CPTs when outputting its widgets.

For reference, the damage was being done by a pre_get_posts hook. Worth checking this if you're similarly stuck.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.