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
- Using wp_query is it possible to orderby taxonomy?
- Get Posts Under Custom Taxonomy
- “tax_query” parameter not working with WP_Query
- How do I exclude a custom taxonomy from the post loop
- get_posts assigned to a specific custom taxonomy term, and not the term’s children
- How can I get only parent terms?
- How to modify a taxonomy that’s already registered
- Change order of Custom Taxonomy List
- Get the the top-level parent of a custom taxonomy term
- get_the_term_list without links in 3.1
- Inserting terms in an Hierarchical Taxonomy
- get_terms – only top level
- Custom taxonomy terms not showing as list Gutenberg Editor
- Altering the appearance of custom taxonomy inputs
- How to only list the child terms of a taxonomy and not their parents?
- Get term SLUG by term ID
- Is ACF being a honey trap? [closed]
- Get term name from term ID?
- Check if Current Category has Children
- How to use taxonomies on attachments with the new Media Library?
- Hierarchical taxonomy UI
- WordPress taxonomy radio buttons
- How to prevent new terms being added to a custom taxonomy?
- Custom taxonomy, get_the_terms, listing in order of parent > child
- How to remove the tag cloud from custom taxonomy admin page?
- custom taxonomy and pages rewrite slug conflict gives 404
- Inserting a term into a custom taxonomy
- Count posts in custom taxonomy
- How to add images to taxonomies?
- Read-only taxonomy (user can assign term but can’t create or edit existing terms)
- Remove taxonomy base or term from url
- Can you add the visual editor to the description field for custom taxonomies?