Make WordPress Core

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#40316 closed defect (bug) (invalid)

wp_count_posts() giving wrong result

Reported by: subrataemfluence's profile subrataemfluence Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.7.3
Component: Posts, Post Types Keywords:
Focuses: Cc:


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 */

The problem is

$post_count = wp_count_posts();

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)

search-result.png (336.8 KB) - added by subrataemfluence 7 years ago.
Record count always displays 2

Download all attachments as: .zip

Change History (5)

7 years ago

Record count always displays 2

#1 @subrataemfluence
7 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 @subrataemfluence
7 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.

#3 @SergeyBiryukov
7 years ago

  • Component changed from General to Posts, Post Types
  • Keywords reporter-feedback removed

#4 @swissspidy
7 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.