Pre_Get_Posts在自定义发布类型上提供404

时间:2013-02-18 作者:Matt Av

我试图在一个名为“提问”的子页面上过滤查询,以列出用户最近提交的所有问题,但在进入该页面时,我得到了404。刷新permalink结构并没有解决该问题。当前设置为月和名称。

创建一个新问题可以正常工作,我可以访问新帖子。自定义分类法也可以正常工作,但归档页面不能正常工作。

这是我正在使用的代码。

Post type

function question_post_type() {
    $labels = array(
        \'name\'                => _x( \'Questions\', \'Post Type General Name\', \'text_domain\' ),
        \'singular_name\'       => _x( \'Question\', \'Post Type Singular Name\', \'text_domain\' ),
        \'menu_name\'           => __( \'Questions\', \'text_domain\' ),
        \'parent_item_colon\'   => __( \'Parent Question:\', \'text_domain\' ),
        \'all_items\'           => __( \'All Questions\', \'text_domain\' ),
        \'view_item\'           => __( \'View Question\', \'text_domain\' ),
        \'add_new_item\'        => __( \'Add New Question\', \'text_domain\' ),
        \'add_new\'             => __( \'New Question\', \'text_domain\' ),
        \'edit_item\'           => __( \'Edit Question\', \'text_domain\' ),
        \'update_item\'         => __( \'Update Question\', \'text_domain\' ),
        \'search_items\'        => __( \'Search questions\', \'text_domain\' ),
        \'not_found\'           => __( \'No questions found\', \'text_domain\' ),
        \'not_found_in_trash\'  => __( \'No questions found in Trash\', \'text_domain\' ),
    );

    $rewrite = array(
        \'slug\'                => \'/ask/question\',
        \'with_front\'          => true,
        \'pages\'               => true,
        \'feeds\'               => true,
    );

    $args = array(
        \'label\'               => __( \'question\', \'text_domain\' ),
        \'description\'         => __( \'Question\', \'text_domain\' ),
        \'labels\'              => $labels,
        \'supports\'            => array( \'title\', \'editor\', \'author\', \'comments\', \'revisions\', \'custom-fields\', \'page-attributes\', ),
        \'hierarchical\'        => false,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_position\'       => 20,
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'rewrite\'             => $rewrite,
        \'capability_type\'     => \'post\',
    );

    register_post_type( \'question\', $args );
}

add_action( \'init\', \'question_post_type\', 0 );

Taxonomy

if ( ! function_exists(\'custom_question_taxonomy\') ) {

    // Register Custom Taxonomy
    function custom_question_taxonomy()  {
        $labels = array(
            \'name\'       

            => _x( \'Question Types\', \'Taxonomy General Name\', \'text_domain\' ),
        \'singular_name\'              => _x( \'Question Type\', \'Taxonomy Singular Name\', \'text_domain\' ),
        \'menu_name\'                  => __( \'Question Type\', \'text_domain\' ),
        \'all_items\'                  => __( \'All Question Types\', \'text_domain\' ),
        \'parent_item\'                => __( \'Parent Question Type\', \'text_domain\' ),
        \'parent_item_colon\'          => __( \'Parent Question Type:\', \'text_domain\' ),
        \'new_item_name\'              => __( \'New Question Type\', \'text_domain\' ),
        \'add_new_item\'               => __( \'Add Question Type\', \'text_domain\' ),
        \'edit_item\'                  => __( \'Edit Question Type\', \'text_domain\' ),
        \'update_item\'                => __( \'Update Question Type\', \'text_domain\' ),
        \'separate_items_with_commas\' => __( \'Separate Question Types with commas\', \'text_domain\' ),
        \'search_items\'               => __( \'Search Question Type\', \'text_domain\' ),
        \'add_or_remove_items\'        => __( \'Add or remove Question Types\', \'text_domain\' ),
        \'choose_from_most_used\'      => __( \'Choose from the most used Question Types\', \'text_domain\' ),
    );

    $rewrite = array(
        \'slug\'                       => \'question-type\',
        \'with_front\'                 => false,
        \'hierarchical\'               => true,
    );

    $args = array(
        \'labels\'                     => $labels,
        \'hierarchical\'               => true,
        \'public\'                     => true,
        \'show_ui\'                    => true,
        \'show_admin_column\'          => true,
        \'show_in_nav_menus\'          => true,
        \'show_tagcloud\'              => true,
        \'rewrite\'                    => $rewrite,
    );

    register_taxonomy( \'question-type\', \'question\', $args );
}


    add_action( \'init\', \'custom_question_taxonomy\', 0 );

}

Query

function question_posts_query( $query ) {

    if ( $query->is_post_type_archive( \'question\' ) || $query->is_page(3001) && $query->is_main_query() ) {
        $query->set( \'post_type\', \'question\' );
    }
    return $query;
}
add_filter( \'pre_get_posts\', \'question_posts_query\' );

My page loop

<?php if (have_posts()) :
    while (have_posts() ) : the_post(); ?>

    <div class="entry">
        <div class="post-content">

          <?php the_content(__(\'Read More &raquo;\', \'gp_lang\')); ?>

          <?php the_terms( $post->ID, \'question-type\', \'<div class="category"><span>Category: </span>\', \'\', \'</div>\' ); ?>
        </div>
    </div>

    <?php endwhile; ?>
<?php endif; ?>

1 个回复
SO网友:Milo

更改的查询page post类型viapre_get_posts 这有点棘手,可能不值得考虑潜在的问题。如果检查生成的SQL,它会在question post类型,这就是您获得404的原因。

解决方案是删除页面,这样WordPress将在默认情况下加载一个post类型的存档,或者保留页面并使用WP_Query 在模板中加载自定义帖子。

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post