自定义发布类型草稿预览转到404

时间:2017-10-20 作者:Kevin M

我在自定义插件中创建了一个自定义帖子类型。

以下是我的帖子类型定义:

function hrd_record_custom_post_type() {

    $args = array(
        \'description\'         => __( \'hrdrecord\', \'hrd_record\' ), 
        \'public\'              => true, 
        \'publicly_queryable\'  => true, 
        \'exclude_from_search\' => false, 
        \'show_in_nav_menus\'   => true, 
        \'show_ui\'             => true, 
        \'show_in_menu\'        => true, 
        \'show_in_admin_bar\'   => true, 
        \'menu_position\'       => null, 
        \'menu_icon\'           => null, 
        \'can_export\'          => true, 
        \'delete_with_user\'    => false, 
        \'hierarchical\'        => false, 
        \'has_archive\'         => true, 
        \'query_var\'           => true, 
        \'capability_type\'     => \'hrdrecord\', 
        \'map_meta_cap\'        => true, 
        \'capabilities\' => array(

            \'edit_post\'              => \'edit_hrdrecord\',
            \'read_post\'              => \'read_hrdrecord\',
            \'delete_post\'            => \'delete_hrdrecord\',
            \'create_posts\'           => \'create_hrdrecords\',
            \'edit_posts\'             => \'edit_hrdrecords\',
            \'edit_others_posts\'      => \'edit_others_hrdrecords\',
            \'publish_posts\'          => \'publish_hrdrecords\',
            \'read_private_posts\'     => \'read_private_hrdrecords\',
            \'read\'                   => \'read\',
            \'delete_posts\'           => \'delete_hrdrecords\',
            \'delete_private_posts\'   => \'delete_private_hrdrecords\',
            \'delete_published_posts\' => \'delete_published_hrdrecords\',
            \'delete_others_posts\'    => \'delete_others_hrdrecords\',
            \'edit_private_posts\'     => \'edit_private_hrdrecords\',
            \'edit_published_posts\'   => \'edit_published_hrdrecords\'
        ),

        \'rewrite\' => array(
            \'slug\'       => \'hrdrecord\', 
            \'with_front\' => false, 
            \'pages\'      => true, 
            \'feeds\'      => true, 
            \'ep_mask\'    => EP_PERMALINK, 
        ),


        \'taxonomies\' => array( \'location\',\'sex\',\'type-of-work\',\'type-of-killing\',\'previous-threat\', \'gender-component\', \'status-of-investigation\',\'sector\',\'sector-detail\'),
        \'supports\' => array(
            \'author\',
            \'thumbnail\',
        ),

        \'labels\' => array(
            \'name\'               => __( \'HRD Record\',                   \'hrd_record\' ),
            \'singular_name\'      => __( \'HRD Record\',                    \'hrd_record\' ),
            \'menu_name\'          => __( \'HRD Record\',                   \'hrd_record\' ),
            \'name_admin_bar\'     => __( \'HRD Record\',                   \'hrd_record\' ),
            \'add_new\'            => __( \'Add New\',                    \'hrd_record\' ),
            \'add_new_item\'       => __( \'Add New HRD Record\',            \'hrd_record\' ),
            \'edit_item\'          => __( \'Edit HRD Record\',               \'hrd_record\' ),
            \'new_item\'           => __( \'New HRD Record\',                \'hrd_record\' ),
            \'view_item\'          => __( \'View HRD Record\',               \'hrd_record\' ),
            \'search_items\'       => __( \'Search HRD Records\',            \'hrd_record\' ),
            \'not_found\'          => __( \'No HRD Record found\',          \'hrd_record\' ),
            \'not_found_in_trash\' => __( \'No HRD Record in trash\',       \'hrd_record\' ),
            \'all_items\'          => __( \'All HRD Records\',               \'hrd_record\' ),

            \'archive_title\'      => __( \'hrdrecord\',                   \'hrd_record\' ),
        )
    );

    register_post_type(
        \'hrdrecord\', 
        $args      
    );


}
add_action( \'init\', \'hrd_record_custom_post_type\' );
我遇到的问题是,当我在草稿中有一个帖子类型时,我单击预览按钮(从所有HRD记录列表或从个人HRD记录编辑屏幕),我会进入404未找到页面。但是如果发布了帖子,我在更新之前预览更改或查看帖子类型时没有问题。

另一件奇怪的事是,当我尝试从列表中预览时,URL的链接中有查询参数?post\\u type=hr记录(&U);p=999(&;预览=true,但一旦此选项出现在浏览器中,则;preview=true查询变量消失,即使我尝试手动重新键入它,一旦单击return,它也会从URL中删除(只保留?post\\u type=hrdrecord&;p=999)。

当我在单个屏幕上尝试预览按钮时,这个变量字符串被重写为?post\\u type=hr记录(&U);p=999(&;预览\\u id=999(&U);preview\\u nonce=一些数字和字母集,但仍会转到404未找到的页面。

我在这里和其他论坛上找到了很多关于类似问题的东西,但都没有解决。

我尝试过更改主题,我尝试过删除帖子类型定义中设置为默认值的参数,我多次重置了永久链接(我的插件也有一个flush\\u rewrite\\u rules();在激活和停用挂钩中,我已经停用并重新激活了插件几次)我已经将“重写”中的“with\\u front”参数更改为true,我已经禁用了所有其他插件,所以我现在不知所措。我假设它与重写和自定义帖子类型定义中的某些参数有关,但我不知道是哪一个或如何让Wordpress将预览查询变量与帖子类型关联。

如有任何建议,将不胜感激。

1 个回复
SO网友:Kevin M

我让一位同事查看了整个插件代码,他发现了我编写的一个函数,该函数强制查询只显示“已发布”的帖子。这会干扰预览,因为它被视为前端查询。我可以通过添加参数绕过它!$query->is_preview() 根据我的情况,现在已经分类了。

结束