WordPress.org

Ready to get started?Download WordPress

Forums

Bug with WordPress get_the_category or get_category? (7 posts)

  1. chasporter
    Member
    Posted 3 years ago #

    Any ideas on what's going on here and how to fix it? I'm using WordPress 3.2 and a variety of plug-ins, Category Checklist Tree being the only one that deals with categories, which is where my problem occurs. Note: when I deactivate Category Checklist Tree I still have the following problem.

    I have the following post category grandparent->parent->child structure

    In the classroom
    	First Grade
    		Aleph
    		Bet
    		Gimel
    	Second Grade
    		Aleph
    		Bet
    		Gimel
    	Third Grade
    		Aleph
    		Bet
    		Gimel
    	Fourth Grade
    		Aleph
    		Bet
    		Gimel

    When I run the following code:

    $ID = $wp_query->posts[0]->cat_ID;
    $postcat = get_the_category($ID);
    $cat = $postcat[0]->cat_ID;
    $thiscat = get_category($cat);
    print_r($thiscat);

    on a single page with a post categorized:

    In the classroom
    	Second Grade
    		Gimel

    I get:

    stdClass Object ( [term_id] => 44 [name] => Gimel [slug] => gimel-second-grade [term_group] => 0 [term_taxonomy_id] => 44 [taxonomy] => category [description] => [parent] => 28 [count] => 1 [object_id] => 765 [cat_ID] => 44 [category_count] => 1 [category_description] => [cat_name] => Gimel [category_nicename] => gimel-second-grade [category_parent] => 28 )

    Which is the result I'd expect and consider correct. However, when I run the same on a single page with a post categorized:

    In the classroom
    	First Grade
    		Gimel

    or

    In the classroom
    	Fourth Grade
    		Gimel

    I get:

    stdClass Object ( [term_id] => 63 [name] => First Grade [slug] => first-grade [term_group] => 0 [term_taxonomy_id] => 63 [taxonomy] => category [description] => [parent] => 26 [count] => 1 [object_id] => 760 [cat_ID] => 63 [category_count] => 1 [category_description] => [cat_name] => First Grade [category_nicename] => first-grade [category_parent] => 26 )

    Which is wrong: the [name] should be Gimel, the [slug] should be gimel-first-grade, etc.

    Noticing that both problem categories begin with "F", I've run a test where I've changed the category "First Grade" to "Thirst Grade" and I then get the correct results: Gimel and gimel-first-grade. I've run additional tests renaming the category to "G", "F", "E", "D", "C", "B" & "A" and each generates the WRONG result: First Grade & first-grade. When I change the category name first letter to any letter other than A, B, C, D, E, F, & G I get the correct result. Also note that when I run this code on a single page with a post categorized:

    In the classroom
    	First Grade (or Fourth Grade)
    		Aleph (or Bet)

    I get the correct result: [name] is Aleph (or Bet) and [slug] is correct as well.

    I've looked at the database and all of the category parent/child relationships are correct.

    It appears that there's some sort of bug with get_the_category or get_category. Any ideas on what's going on here and how to fix it?

    Thanks in advance.

  2. nsathees
    Member
    Posted 2 years ago #

    No there is no bug with that! Just simply say what are you trying to archive?

    Are you trying to get the category of the post? If yes it can be archived much easier and clearer than your code!

  3. alchymyth
    Forum Moderator
    Posted 2 years ago #

    http://codex.wordpress.org/Function_Reference/get_the_category

    afaik, returns the categories sorted by name, ASC

  4. Guillaume Assire
    Member
    Posted 2 years ago #

    Has anybody ever noticed that a category is returned by these fonctions _only_ if there's at least one post categorized in it ? Not sure if this is really wanted...?

  5. esmi
    Forum Moderator
    Posted 2 years ago #

    Returns an array of objects, one object for each category assigned to the post.

    http://codex.wordpress.org/Function_Reference/get_the_category

    Makes perfect sense to me.

  6. Guillaume Assire
    Member
    Posted 2 years ago #

    Sorry, I meant get_categories(), my mistake... I never got it to list a category with no post in it. Of course this is not quite nominal, but still could be useful...

  7. Guillaume Assire
    Member
    Posted 2 years ago #

    Regarding get_categories, and although it has nothing to do with the inital issue, I thought I should let you know that after all these months, I finally got it...

    hide_empty
    (boolean) Toggles the display of categories with no posts. The default is 1 for true or you can add '0' for false (show empty categories). Valid values:

    * 1 - default
    * 0

    AG

Topic Closed

This topic has been closed to new replies.

About this Topic