You need to use get_ancestors()
.
Assuming your post is only in one category, the following code should work (if it’s in multiple you’ll need to loop through each of the assigned categories to determine the various hierarchies).
$category = get_the_category();
$ancestors = get_ancestors( $category[0]->term_id, 'category' );
$direct_parent_id = $ancestors[0];
If you want to get the entire category hierarchy as an ordered array of IDs (which I like to have available) you’d do:
$category = get_the_category();
$hierarchy = array_reverse( get_ancestors( $category[0]->term_id, 'category' ) );
$hierarchy[] = $category[0]->term_id;
Related Posts:
- Retrieve posts by term id custom query
- Display category posts grouped by taxonomy
- restrict_manage_posts not working in 3.3.1
- Filter all queries with a specific taxonomy
- wp_query orderby title and meta key value (WP3.1)
- Looping Through Custom Tax Terms and Displaying All Posts For Each
- get / list categories and counts filtered by custom taxonomy term
- Taxonomy.php issue with search and filters
- How to List Parent Term Links for Custom Taxonomy With & Without Children?
- Recreating the hierarchy of taxonomies for a dropdown form menu?
- “show option all” on list categories doesn’t display taxonomy
- Querying Term Posts in Loop
- Custom taxonomy query not working with switch_to_blog
- display taxonomy slug from term ID
- $wpdb query a post type within a specific taxonomy term while ordering posts by custom meta value?
- Using wp_list_categories to show more than one custom taxonomy
- Print terms with taxonomy and metabox value
- Display Taxonomy Terms in an option tag with value being the slug
- Checkbox onclick filtering on the same page
- Category list with indent children list below current category
- A way to query custom taxonomies by name
- get the taxonomies terms associated with users
- Widget dropdown always displays first option
- I am trying to hide a custom post type category to logged in users with Pre_Get_Posts
- Rewrite Rules returning wrong data
- Sorting main query by custom taxonomy slug
- WordPress Custom post query sorting does not work
- List related terms + taxonomies
- Search Query for Multiple Terms In Same Taxonomy
- What does ‘category__in’ mean in queries?
- Exclude taxonomy term from all loops, but having it on widget
- Trying to add taxonomy to get_categories() but it’s not working. How to fix this?
- Proper WP conditional tag to check for existing taxonomies to list out terms with wp_list_categories?
- Limit Display Number Of Taxonomy Term
- Get list of taxonomies associated with users
- query_posts that have custom taxonomy and limiting what shows based on the taxonomy
- Several taxonomy query (like filter)
- Taxonomy archive template that shows posts from more than one taxonomy
- how to access the $query variable inside taxonomy-xxx.php template file?
- Displaying Posts Attached to Custom Taxonomy Terms
- Update Custom Taxonomy Value for a post based on custom date field
- Show single Child Category on Custom Post
- How to get the term description in a taxonomy term archive query?
- making an index from custom taxonomies and tags of posts (not hierarchical taxonomy)
- Query Multiple Custom Taxonomies
- How to divide Subcategories into pages of parent category wordpress
- List of post categories only associated to another custom taxonomy
- How to show category list in WordPress
- List all taxonomies with their descriptions
- Problem with wp_list_category with custom taxomy
- Query by multiple custom taxonomies
- ACF Custom field not showing in Timber Taxonomy page
- Add and Custom category for posts “PAGE”
- WP Query : strange behaviour with multiple no-hierarchical tax
- WordPress search form and search result through ACF field in custom taxonomy
- Query pages by child term
- Can I use multi (sub) levels of relation (AND | OR) on custom query?
- Tax Query not working
- Custom taxonomy – query returns an error
- Custom taxonomy list in Worpdress
- How to perform a search inside a specific taxonomy category
- Adding a query var to taxonomy term archive – gets redirected to the other taxonomy archive page
- add_query_arg to look up page title
- Displaying Custom Taxonomy List Posts By Slug?
- Can I use $query->set() (in a pre_get_posts() hook) with a custom taxonomy in WP 3?
- How can I query for all values of a custom taxonomies?
- query grandchildren taxonomy terms
- custom texonomy category child category list with parent head
- Get the the top-level parent of a custom taxonomy term
- get_terms – only top level
- Filtering a custom post type by custom taxonomy in archive template
- Order get_terms using a Custom Field
- How to modify the query in taxonomy-custom.php to sort term archives by a custom meta field?
- wp_options table value
- Custom Taxonomy Meta Admin Column
- Custom Taxonomy Only Showing Top Level Terms in Admin?
- custom taxonomies on permalink
- Landing in 404 Page on Navigating to Custom Taxonomy Archive
- Get main term from a sub term in Woocommerce
- Generic taxonomy-term template page
- How to count the number of terms in a taxonomy
- How to filter custom taxonomy categories on archive?
- Current category link filter
- Display only child terms of a specific parent term of a custom taxonomy, for each custom post
- Are exclusive taxonomy terms possible?
- Some custom taxonomy child terms not showing in admin. Caching problem?
- Custom Taxonomy Query
- My custom taxonomy page is leading in 404 page
- List Top 5 taxonomy terms based on number of post
- Custom types, taxonomies and query optimization
- Taxonomy.php how to show post only in current taxonomy with wp_query?
- Query custom posts from custom taxonomy
- Retrieve the child terms by having the parent’s information
- Why get_terms() behaves strangely when being called in admin (for use in meta box)?
- ‘Post_tag’ taxonomy tags don’t link to existing posts in CMS
- Sorting terms individually for each post
- Looking for suggestions on creating simple database (Help!!)
- get meta fields and taxonomy of any post type
- Getting grandchildren of a post with a specified custom taxonomy?
- Can you orderby slug in a taxonomy archive page using WP_Query?