#40316 closed defect (bug) (invalid)
wp_count_posts() giving wrong result
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.7.3 |
Component: | Posts, Post Types | Keywords: | |
Focuses: | Cc: |
Description
I have built a customized search for my website. In searchform.php I have added multiple custom posts types I want to consider in search result. Also I customized the search.php as well in the following manner:
if(have_posts()) { $post_count = wp_count_posts(); echo $post_count->publish; while(have_posts()) : the_post(); $postType = get_post_type(get_the_ID()); if($postType == 'trips') { /* HTML markup */ } elseif($postType == 'travelog') { /* HTML markup */ } ... ... endwhile; }
The problem is
$post_count = wp_count_posts(); $post_count->publish
always gives me '2', but I know there are more records in the object (have_posts()
). When I use a counter and increment it inside while ... endwhile
, the count displays right.
Attachments (1)
Change History (5)
#1
@
8 years ago
var_dump
output:
object(stdClass)#1847 (12) { ["publish"]=> string(1) "2" ["future"]=> int(0) ["draft"]=> int(0) ["pending"]=> int(0) ["private"]=> int(0) ["trash"]=> int(0) ["auto-draft"]=> string(1) "1" ["inherit"]=> int(0) ["closed"]=> int(0) ["spam"]=> int(0) ["orphan"]=> int(0) ["hidden"]=> int(0) }
It is not considering all POST TYPES considered in search result?
#2
@
8 years ago
- Keywords reporter-feedback added
- Resolution set to invalid
- Status changed from new to closed
This is not relevant as wp_count_posts
originally returns a number of posts within a specific post type. If no post type is passed 'POST' is taken as default. That's why I am getting 2. Closing this ticket as invalid! wp_count_posts
does not count how many records are present inside a WordPress loop.
Record count always displays 2