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 |1 Answer
Reset to default 1Usually 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
<input type="hidden" name="post_type" value="cpt goes here" />
. You can swap it for a dropdown or radio buttons etc as long as thename
ispost_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 valuesWP_Query
accepts – Tom J Nowell ♦ Commented Aug 12, 2020 at 14:14