如何显示未来的帖子-修改后的查询仍会生成404

时间:2014-09-05 作者:Øle Bjarnstroem

我有一个非常奇怪的问题,我被卡住了。我希望单个页面显示post\\u状态为publish and future的帖子。因此,我将此代码添加到我的函数中。我的主题中的php:

function display_future_posts() {
    global $wp_query;
    if( !is_admin() && $wp_query->is_main_query() && $wp_query->is_single()) {
        $wp_query->set( \'post_status\', array(\'future\', \'publish\', \'draft\') );
    }
}
add_action(\'pre_get_posts\', \'display_future_posts\');
这将成功更改查询。但当我没有以管理员身份登录时,仍然会看到404页面。所以我跟踪了db查询。下面是我在调用print\\u r($wpdb->querys)时发现的内容;:

[16] => Array
    (
        [0] => SELECT   wp_posts.* FROM wp_posts  WHERE 1=1  AND ( ( YEAR( post_date ) = 2014 AND MONTH( post_date ) = 9 ) ) AND wp_posts.post_name = \'my-life\' AND wp_posts.post_type = \'post\' AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'future\')  ORDER BY wp_posts.post_date DESC 
        [1] => 0.000303030014038
        [2] => require(\'wp-blog-header.php\'), wp, WP->main, WP->query_posts, WP_Query->query, WP_Query->get_posts
    )

[17] => Array
    (
        [0] => SELECT post_id FROM wp_postmeta, wp_posts WHERE ID = post_id AND post_type = \'post\' AND meta_key = \'_wp_old_slug\' AND meta_value = \'my-life\' AND YEAR(post_date) = 2014 AND MONTH(post_date) = 9
        [1] => 0.000878095626831
        [2] => require(\'wp-blog-header.php\'), require_once(\'wp-includes/template-loader.php\'), do_action(\'template_redirect\'), call_user_func_array, wp_old_slug_redirect
    )

[18] => Array
    (
        [0] => SELECT ID FROM wp_posts WHERE post_name LIKE \'my-life%\' AND post_type IN (\'post\', \'page\', \'attachment\') AND YEAR(post_date) = 2014 AND MONTH(post_date) = 9 AND post_status = \'publish\'
        [1] => 0.00031304359436
        [2] => require(\'wp-blog-header.php\'), require_once(\'wp-includes/template-loader.php\'), do_action(\'template_redirect\'), call_user_func_array, redirect_canonical, redirect_guess_404_permalink
    )
因此,执行了我的查询,但wp中的函数wp\\u old\\u slug\\u redirect()包含/query。调用php,函数is\\u 404()返回true。所以Wordpress试图找到一个旧的slug(当然不存在),失败后重定向到404。

当我调用print\\u r($wp\\u query)时;在标题中。我的主题php似乎成功修改了查询,但奇怪的是没有返回任何结果。至少[帖子]为空。当我直接在MySQL服务器上运行[请求]时,我得到的正是我想要的结果。

WP_Query Object
(
    [query] => Array
        (
            [page] => 
            [year] => 2014
            [monthnum] => 09
            [name] => my-life
        )

    [query_vars] => Array
        (
            [page] => 0
            [year] => 2014
            [monthnum] => 9
            [name] => my-life
            [error] => 
            [m] => 
            [p] => 0
            [post_parent] => 
            [subpost] => 
            [subpost_id] => 
            [attachment] => 
            [attachment_id] => 0
            [static] => 
            [pagename] => 
            [page_id] => 0
            [second] => 
            [minute] => 
            [hour] => 
            [day] => 0
            [w] => 0
            [category_name] => 
            [tag] => 
            [cat] => 
            [tag_id] => 
            [author] => 
            [author_name] => 
            [feed] => 
            [tb] => 
            [paged] => 0
            [comments_popup] => 
            [meta_key] => 
            [meta_value] => 
            [preview] => 
            [s] => 
            [sentence] => 
            [fields] => 
            [menu_order] => 
            [category__in] => Array
                (
                )

            [category__not_in] => Array
                (
                )

            [category__and] => Array
                (
                )

            [post__in] => Array
                (
                )

            [post__not_in] => Array
                (
                )

            [tag__in] => Array
                (
                )

            [tag__not_in] => Array
                (
                )

            [tag__and] => Array
                (
                )

            [tag_slug__in] => Array
                (
                )

            [tag_slug__and] => Array
                (
                )

            [post_parent__in] => Array
                (
                )

            [post_parent__not_in] => Array
                (
                )

            [author__in] => Array
                (
                )

            [author__not_in] => Array
                (
                )

            [meta_query] => Array
                (
                )

            [post_status] => Array
                (
                    [0] => future
                    [1] => publish
                )

            [ignore_sticky_posts] => 
            [suppress_filters] => 
            [cache_results] => 1
            [update_post_term_cache] => 1
            [update_post_meta_cache] => 1
            [post_type] => 
            [posts_per_page] => 7
            [nopaging] => 
            [comments_per_page] => 50
            [no_found_rows] => 
            [order] => DESC
        )

    [tax_query] => 
    [meta_query] => WP_Meta_Query Object
        (
            [queries] => Array
                (
                )

            [relation] => 
        )

    [date_query] => 
    [request] => SELECT   wp_posts.* FROM wp_posts  WHERE 1=1  AND ( ( YEAR( post_date ) = 2014 AND MONTH( post_date ) = 9 ) ) AND wp_posts.post_name = \'my-life\' AND wp_posts.post_type = \'post\' AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'future\')  ORDER BY wp_posts.post_date DESC 
    [posts] => Array
        (
        )

    [post_count] => 0
    [current_post] => -1
    [in_the_loop] => 
    [comment_count] => 0
    [current_comment] => -1
    [found_posts] => 1
    [max_num_pages] => 0
    [max_num_comment_pages] => 0
    [is_single] => 
    [is_preview] => 
    [is_page] => 
    [is_archive] => 
    [is_date] => 
    [is_year] => 
    [is_month] => 
    [is_day] => 
    [is_time] => 
    [is_author] => 
    [is_category] => 
    [is_tag] => 
    [is_tax] => 
    [is_search] => 
    [is_feed] => 
    [is_comment_feed] => 
    [is_trackback] => 
    [is_home] => 
    [is_404] => 1
    [is_comments_popup] => 
    [is_paged] => 
    [is_admin] => 
    [is_attachment] => 
    [is_singular] => 
    [is_robots] => 
    [is_posts_page] => 
    [is_post_type_archive] => 
    [query_vars_hash] => 7ce0208af83c782976286abc2d021484
    [query_vars_changed] => 1
    [thumbnails_cached] => 
    [stopwords:WP_Query:private] => 
)
我没脑子了。我做错了什么?我错过了什么。

我希望这个问题不要太具体,它对其他人有帮助。

谢谢Ole

2 个回复
SO网友:Rarst

你踩到了一个查询地雷。很容易假设:

您可以更改查询实际上有很多查询参数,涉及到这些参数会导致可怕的死胡同。WordPress设计用于查询和显示公开的帖子。任何试图绕过它的行为都会带来严重的悲痛。

在您的特定情况下,这就是生成的SQL查询的样子(对于草稿帖子):

SELECT wp_posts.*
FROM wp_posts
WHERE 1=1 AND wp_posts.post_name = \'draft\' 
    AND wp_posts.post_type = \'post\' 
    AND ((
        wp_posts.post_status = \'publish\' 
        OR wp_posts.post_status = \'future\' 
        OR wp_posts.post_status = \'draft\'
        ))
ORDER BY wp_posts.post_date DESC
看起来很正常,不是吗?嗯,除了那篇题为“草稿”的帖子doesn\'t have post_name field set to anything.

这不是“帖子总是帖子”的情况。这是“帖子将有细微的不同,充满边缘案例”的情况。

每当你需要更复杂的概念时,比如“未来”帖子,do not trust WP core to do it. 它会让你开怀大笑,然后毁掉你未来三天的生活。

将复杂数据实现为自定义字段数据

SO网友:Remy Sheppard

它不会让我发表评论,因为我还不够酷。

您是否考虑过使用wordpress的WP\\u查询来更改循环,而不是创建函数?

$new_query = WP_Query(
    \'post-status\' => array (
        \'future\',
        \'publish\',
        \'draft\',
    ),
);

if ( $new_query->have_posts() ) :
    while ($new_query->have_posts() ) : the_post(); ?>
        <h1><?php echo the_title(); ?></h1>
    <?php endwhile;
endif;
如果您需要它仅位于特定页面上,则始终可以创建页面-[模板名称]。php,然后是内容-[模板名称]。php来调用它。

如果您在这里追求完全不同的内容,请告诉我,但如果您只是想在页面上显示不同的帖子状态类型,我会更改循环。

结束

相关推荐

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

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