最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Empty search input does not return all posts

programmeradmin1浏览0评论

I am working on WordPress custom post type search functionality. When I have searched with an empty value, it does not return all posts. When I am using this below code:

global $wp_query; 
echo $wp_query->found_posts;

It returns only 15, but I have 380 posts. Can anyone please advise me on how to solve this.

I am working on WordPress custom post type search functionality. When I have searched with an empty value, it does not return all posts. When I am using this below code:

global $wp_query; 
echo $wp_query->found_posts;

It returns only 15, but I have 380 posts. Can anyone please advise me on how to solve this.

Share Improve this question edited Aug 14, 2020 at 23:54 Nate Allen 2,1062 gold badges16 silver badges23 bronze badges asked Aug 12, 2020 at 12:40 SomyaSomya 1 1
  • Note that you can implement CPT search without making a custom page template and a wasteful second query by putting this in your search form: <input type="hidden" name="post_type" value="cpt goes here" />. You can swap it for a dropdown or radio buttons etc as long as the name is post_type and the value is the CPT you want. You can even have a comma separated list! The same is true of all the other values WP_Query accepts – Tom J Nowell Commented Aug 12, 2020 at 14:14
Add a comment  | 

1 Answer 1

Reset to default 1

Usually this is because by default WP_Query will return the results in pages, according to your Wordpress page length setting. So probably your page length is set to 15 and you're just seeing the first page.

You should look into pagination, or you can do a new WP_Query with posts_per_page set to -1 to ensure you get all the posts.

More info on how to use WP_Query is here: https://developer.wordpress/reference/classes/wp_query/#pagination-parameters , or provide more info on what and where you're trying to do this if you want more help on a specific case

发布评论

评论列表(0)

  1. 暂无评论