404适用于分层自定义帖子类型中的孩子

时间:2014-01-17 作者:Steve Taylor

我已经尝试了所有我能找到的解决方案,但我仍然被困在这里。This post 有一个很好的彻底的调试方法,但它没有帮助。

部分原因可能是因为我想the Flatpack theme 改变它的行为。我们现在锁定了这个主题。我这样做是为了使投资组合和服务CPT分层:

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

}

}
据我所知,这可以确保所有相关的CPT设置都是正确的(包括设置query_var 这似乎是这个问题的常见“解决方案”)。上面末尾的portfolio CPT对象转储给出了以下结果:

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
)
刷新规则,仍然:

http://languagefutures.localhost/portfolio/project-and-themes/ (正常工作)http://languagefutures.localhost/portfolio/project-and-themes/who-do-you-think-you-are/ (404)

我在后一个URL上使用了重写规则检查器插件,它显示:

portfolio/(.+?)(/[0-9]+)?/?$    index.php?portfolio=$matches[1]&page=$matches[2]    portfolio
(.?.+?)(/[0-9]+)?/?$            index.php?pagename=$matches[1]&page=$matches[2] page
好吧,这一切都是应该的。使用查询监视器插件,以下是正在运行的相关查询:

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
最引人注目的是第二个。1493确实是子页面的正确ID。为什么查询指定post\\u parent=0?我将继续调查此事,但如果有人经历过这样的事情并幸存下来,那么给他们一些提示就太好了。

1 个回复
SO网友:Steve Taylor

事实证明,这是另一个扁平封装黑客的干扰,我在那里阻止它在输出小部件时显示子CPT。

作为参考,损坏是由pre_get_posts 钩如果你同样陷入困境,值得检查一下。

结束

相关推荐

在主页上混合使用常规和自定义帖子类型(带有meta_Query)

不知道如何实现这一点。我试图在网站主页上混合标准帖子和自定义帖子,但我只想在设置了元值的情况下显示自定义帖子。显示帖子效果良好\'post_type\' => array(\'game\', \'post\') 但是,当我添加meta\\u查询时,常规帖子不再显示(这很有意义,因为它们不符合meta\\u查询条件)。那么,如何将meta\\u查询限制为仅自定义帖子类型,以便仍然包含常规帖子?