【问题标题】:WordPress: How do I get all posts from $wp_query in the search results?WordPress:如何在搜索结果中获取来自 $wp_query 的所有帖子?
【发布时间】:2015-08-12 10:32:48
【问题描述】:

我一定是脑残了,我不知道如何从$wp_query 获取所有帖子,以便为搜索结果创建一个小部件过滤器。

$wp_query->posts 只给我将要显示在列表中的帖子,所以,如果posts_per_page 设置为 10,我只会得到 10 个帖子。我需要它们,所以我可以对它们进行排序并根据搜索结果中的所有帖子显示过滤器。

有什么想法吗?

【问题讨论】:

标签: php wordpress wordpress-theming


【解决方案1】:

将 args 中的 posts_per_page 参数设置为 -1,这将返回 wp_posts 表中的所有帖子。例如

$args = array(
    'posts_per_page'   => -1,
    'post_type'        => 'post',
);
$the_query = new WP_Query( $args );

现在您可以循环浏览并获取帖子

while ( $the_query->have_posts() ) {
  // go ahead
}

【讨论】:

  • 是的,但这会显示页面上的所有帖子。我想保持分页。但我也希望能够对结果进行过滤。
  • 但是你不能通过同一个 WP_Query 来做到这一点,你需要为此设置单独的 WP_Query。
【解决方案2】:

根据搜索结果中的所有帖子显示过滤器。

     <?php

    /*pass your search string here example like this ( 's'=>'test' ) */
   $args=array('s'=>'test','order'=> 'DESC', 'posts_per_page'=>get_option('posts_per_page'));

   $query=new WP_Query($args);

    if( $query->have_posts()): 

    while( $query->have_posts()): $query->the_post();

     {
     echo $post->post_title;
     echo $post->post_content;
     }

    endwhile; 
    else:
    endif;
  ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 2011-03-24
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多