Support » Fixing WordPress » Getting a category name from a post…?

  • What I’m looking to do is search a specific subcategory from a post.

    For example:
    Category ‘A’
    – subcategory ‘YY’

    Post ‘DELTA’ has YY category assigned to it.

    I have post DELTA’s ID and need to search for assigned categories under category A

    In SQL it would be this statement:
    “SELECT categories.category_name, sub_category.category_name, posts.[post-name]
    FROM posts INNER JOIN (categories AS sub_category INNER JOIN categories ON sub_category.parent_category = categories.ID) ON posts.category = sub_category.ID
    WHERE (((posts.[post-name])=”DELTA”));”

    I just can’t figure out how to translate this into the querystring.

    Much Thanks!
    -Derek

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi,

    Refer this article:

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

    Thanks,

    Shane G.

    Thread Starter i3

    (@i3)

    I’ve looked at this function before but it doesn’t allow you to query categories based on a specific post. It allows you to use the ‘type’ parameter to return categories associated with post or links but not a specific post.

    Any other ideas out there. This can’t be that hard….???

    Thread Starter i3

    (@i3)

    Okay… I figured it out for those of you interested…

    $cat_array = wp_get_post_categories($post->ID);
    
    foreach ($cat_array as $category) {
    
    	if (cat_is_ancestor_of(18, $category))  //Check to see if its an ancestor
    	{
    		// If ancestor do something
    		echo get_cat_name($category);
    	}
    }

    Thread Starter i3

    (@i3)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Getting a category name from a post…?’ is closed to new replies.