[wp-trac] [WordPress Trac] #23829: get_the_category() returns incorrect value if called with no parameters

WordPress Trac noreply at wordpress.org
Wed Mar 20 18:34:34 UTC 2013


#23829: get_the_category() returns incorrect value if called with no parameters
-----------------------------+--------------------------
 Reporter:  norocketsurgeon  |       Type:  defect (bug)
   Status:  new              |   Priority:  normal
Milestone:  Awaiting Review  |  Component:  General
  Version:  trunk            |   Severity:  major
 Keywords:  has-patch        |
-----------------------------+--------------------------
 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

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/23829>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list