如何将WP_QUERY限制为循环上的一个结果?

时间:2016-01-05 作者:ReynierPM

我只想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个帖子,而不是一个。有人能就这个话题给我一些建议吗?我读过thisthis 但一点帮助都没有。

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进行操作?

2 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

pre_get_posts 更改所有查询,无论是后端还是前端。

有两张非常重要的支票,很多人都漏掉了,那就是!is_admin() 检查哪些只针对前端,然后是最重要的检查,is_main_query() 这只会更改主查询,而不会更改自定义查询。

你的陈述应该如下所示

if (    !is_admin() 
     && $query->is_main_query()  
     && $query->is_search()  
) {
    // Your arguments to set
}

SO网友:ReynierPM

好的,在@pieter-goosen 我找到了一个解决方案,只需添加此remove_all_filters( \'pre_get_posts\' ) 在运行之前WP_Query 解决问题,希望对其他人有帮助

相关推荐

Vimeo froogaloop

我是个新手,但我想用this idea 在Wordpress站点上嵌入的Vimeo剪辑中禁用正向搜索我的职能。我添加了phpfunction frogaloop_scripts() { wp_register_script(\'snippet\', \'https://siteurl/wp-content/themes/themename/js/snippet.js\'); wp_register_script(\'frogaloop\',\'https://f.vimeo