Forums

Getting a category name from a post...? (5 posts)

  1. I3
    Member
    Posted 2 years ago #

    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

  2. Shane G
    Member
    Posted 2 years ago #

    Hi,

    Refer this article:

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

    Thanks,

    Shane G.

  3. I3
    Member
    Posted 2 years ago #

    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....???

  4. I3
    Member
    Posted 2 years ago #

    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);
    	}
    }
  5. I3
    Member
    Posted 2 years ago #


Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.