我只想one 发布和此需要the latest one 下面是我正在做的:
$basic_args = $wp_query->query_vars;
$basic_args[\'tax_query\'] = $wp_query->tax_query;
$aditional_args = array(
\'post__in\' => get_option(\'sticky_posts\'),
\'ignore_sticky_posts\' => 1,
\'order\' => \'desc\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 1
);
$normal_args = array_merge($basic_args, $aditional_args);
$normal_query = new WP_Query($normal_args);
$prev_post_ids = array();
if (!$normal_query->have_posts()) {
$normal_args = array_merge($basic_args, array(\'posts_per_page\' => 1));
$normal_query = new WP_Query($normal_args);
}
echo $normal_query->post_count; // returns 25
if ($normal_query->have_posts()) {
while ($normal_query->have_posts()) {
....
// here I got 25 results instead of just one
}
}
我知道
posts_per_page
应该是限制多少职位,我会得到,但不是为我工作,不知道为什么。正如你所看到的,我在循环中得到了25个帖子,而不是一个。有人能就这个话题给我一些建议吗?我读过
this 和
this 但一点帮助都没有。
Update1
我调试
$normal_query->request
根据建议
@pieter-goosen 我得到了错误的SQL查询:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (6) AND tt1.term_taxonomy_id IN (6) ) AND wp_posts.post_type IN (\'opinion\', \'post\', \'especiales\') AND ((wp_posts.post_status = \'publish\')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 25
其中
LIMIT
是否已修改?
Update2
我还调试了
$basic_args
其中有
$wp_query->query_vars
还有
$normal_args
结果如下:
echo \'<pre>\';
print_r($basic_args);
echo \'</pre>\';
Array
(
[category_name] => internacionales
[error] =>
[m] =>
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[static] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[tag] =>
[cat] => 409
[tag_id] =>
[author] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[comments_popup] =>
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[title] =>
[fields] =>
[menu_order] =>
[category__in] => Array
(
)
[category__not_in] => Array
(
)
[category__and] => Array
(
)
[post__in] => Array
(
)
[post__not_in] => Array
(
)
[post_name__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
(
)
[ignore_sticky_posts] =>
[suppress_filters] =>
[cache_results] => 1
[update_post_term_cache] => 1
[update_post_meta_cache] => 1
[post_type] =>
[posts_per_page] => 10
[nopaging] =>
[comments_per_page] => 50
[no_found_rows] =>
[order] => DESC
[tax_query] => WP_Tax_Query Object
(
[queries] => Array
(
[0] => Array
(
[taxonomy] => category
[terms] => Array
(
[0] => internacionales
)
[field] => slug
[operator] => IN
[include_children] => 1
)
)
[relation] => AND
[table_aliases:protected] => Array
(
[0] => wp_term_relationships
)
[queried_terms] => Array
(
[category] => Array
(
[terms] => Array
(
[0] => internacionales
)
[field] => slug
)
)
[primary_table] => wp_posts
[primary_id_column] => ID
)
)
echo \'<pre>\';
print_r($normal_args);
echo \'</pre>\';
Array
(
[category_name] => internacionales
[error] =>
[m] =>
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[static] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[tag] =>
[cat] => 409
[tag_id] =>
[author] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[comments_popup] =>
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[title] =>
[fields] =>
[menu_order] =>
[category__in] => Array
(
)
[category__not_in] => Array
(
)
[category__and] => Array
(
)
[post__in] => Array
(
)
[post__not_in] => Array
(
)
[post_name__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
(
)
[ignore_sticky_posts] => 1
[suppress_filters] =>
[cache_results] => 1
[update_post_term_cache] => 1
[update_post_meta_cache] => 1
[post_type] =>
[posts_per_page] => 1
[nopaging] =>
[comments_per_page] => 50
[no_found_rows] =>
[order] => desc
[tax_query] => WP_Tax_Query Object
(
[queries] => Array
(
[0] => Array
(
[taxonomy] => category
[terms] => Array
(
[0] => internacionales
)
[field] => slug
[operator] => IN
[include_children] => 1
)
)
[relation] => AND
[table_aliases:protected] => Array
(
[0] => wp_term_relationships
)
[queried_terms] => Array
(
[category] => Array
(
[terms] => Array
(
[0] => internacionales
)
[field] => slug
)
)
[primary_table] => wp_posts
[primary_id_column] => ID
)
[post_status] => publish
)
如你所见
post_per_page
设置为
1
这就是为什么要花25美元?
Update 3
显然这就是
functions.php
:
function change_wp_search_size($query)
{
if ($query->is_search) {
$query->query_vars[\'posts_per_page\'] = 25;
} // Change 25 to the number of posts you would like to show
return $query; // Return our modified query variables
}
add_filter(\'pre_get_posts\', \'change_wp_search_size\');
这不应该只适用于搜索吗?为什么总是对所有query\\u vars进行操作?