WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#23829 closed defect (bug) (invalid)

get_the_category() returns incorrect value if called with no parameters

Reported by: norocketsurgeon Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.5
Component: Template Keywords: has-patch
Focuses: Cc:

Description

While the wordpress codex say that if get_the_category is called without any parameters it should get the categories for the current post instead it returns an empty array if called with no parameters. This is due to the following code flow:

get_the_category() calls
get_the_terms( false ) calls
get_post ( false )

get_the_terms expects and integer and passing it to get_post without any checking. get_post expects the parameters passed to it to be an integer. If it is not an integer it returns null. This causes get_the_terms() to return false back to get_the_category() because of the following code in get_the_terms():

if ( ! $post = get_post( $post ) )
                return false;

This causes get_the_category() to return an empty array do to the following code:

if ( ! $categories || is_wp_error( $categories ) )
                $categories = array();

I have included a patch for this.

Steps to replicate this issue:
1) create wordpress site containing a page that has been assigned a category.
2) inside the loop section of your page.php add:
$categories = get_the_category();
print_r($categories);
3) browse to the page that has the category
4) witness the problem

Attachments (1)

get_the_content_fix.diff (525 bytes) - added by norocketsurgeon 5 years ago.
get_the_content patch

Download all attachments as: .zip

Change History (3)

@norocketsurgeon
5 years ago

get_the_content patch

#1 @norocketsurgeon
5 years ago

  • Resolution set to invalid
  • Status changed from new to closed

Sorry guys, This was actually just a problem with the post-to-post plugin that I misinterpreted as a WordPress bug. I made the mistake of looking at the codex for get_post instead of the source for the function. Please ignore.

#2 @SergeyBiryukov
5 years ago

  • Component changed from General to Template
  • Milestone Awaiting Review deleted
  • Severity changed from major to normal
  • Version changed from trunk to 3.5
Note: See TracTickets for help on using tickets.