[wp-trac] [WordPress Trac] #40316: wp_count_posts() giving wrong result

WordPress Trac noreply at wordpress.org
Thu Mar 30 13:26:49 UTC 2017


#40316: wp_count_posts() giving wrong result
------------------------------+-----------------------------
 Reporter:  subrataemfluence  |      Owner:
     Type:  defect (bug)      |     Status:  new
 Priority:  normal            |  Milestone:  Awaiting Review
Component:  General           |    Version:  4.7.3
 Severity:  normal            |   Keywords:
  Focuses:                    |
------------------------------+-----------------------------
 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.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/40316>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list