我有一个非常奇怪的问题,我被卡住了。我希望单个页面显示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