{"id":4693,"date":"2012-07-27T15:05:23","date_gmt":"2012-07-27T15:05:23","guid":{"rendered":"https:\/\/www.techrounder.com\/blog\/wordpress-function-to-get-parent-category-id\/"},"modified":"2026-01-04T11:36:57","modified_gmt":"2026-01-04T11:36:57","slug":"wordpress-function-to-get-parent-category-id","status":"publish","type":"post","link":"https:\/\/www.techrounder.com\/blog\/wordpress-function-to-get-parent-category-id\/","title":{"rendered":"WordPress &#8211; Function to Get Parent Category ID"},"content":{"rendered":"<p>You can retrieve the parent Category id of a post by using the following function.<\/p>\n<pre lang=\"php\" line=\"1\">function get_posts_parent_category_id(){\r\n$category = get_the_category();\r\n$cat = get_category($category[0]->term_id);\r\nreturn $parent_catid = $cat->category_parent;\r\n}<\/pre>\n<p>Place the above code in function.php in your theme folder or place it in the function.php in the wp-include folder (to use the function in any theme).<\/p>\n<p>From any page, simply call the function and get the parent category id in a variable like this.<\/p>\n<pre lang=\"php\" line=\"1\">$parent_catid = get_posts_parent_category_id();<\/pre>\n<p>Now you have the parent category id in <strong>$parent_catid<\/strong> variable<\/p>\n","protected":false},"excerpt":{"rendered":"You can retrieve the parent Category id of a post by using the following function. function get_posts_parent_category_id(){ $category&hellip;","protected":false},"author":2,"featured_media":1482,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"csco_display_header_overlay":false,"csco_singular_sidebar":"","csco_page_header_type":"","csco_page_load_nextpost":"","csco_post_video_location":[],"csco_post_video_location_hash":"","csco_post_video_url":"","csco_post_video_bg_start_time":0,"csco_post_video_bg_end_time":0,"csco_post_video_bg_volume":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-4693","post","type-post","status-publish","format-standard","has-post-thumbnail","category-webtips","cs-entry","cs-video-wrap"],"_links":{"self":[{"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/posts\/4693","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/comments?post=4693"}],"version-history":[{"count":0,"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/posts\/4693\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/media\/1482"}],"wp:attachment":[{"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/media?parent=4693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/categories?post=4693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/tags?post=4693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}