{"id":49,"date":"2009-11-13T17:59:44","date_gmt":"2009-11-13T16:59:44","guid":{"rendered":"http:\/\/wpseek.com\/blog\/?p=49"},"modified":"2022-03-07T09:40:20","modified_gmt":"2022-03-07T08:40:20","slug":"get-id-of-parent-category","status":"publish","type":"post","link":"https:\/\/wpseek.com\/blog\/2009\/get-id-of-parent-category\/49\/","title":{"rendered":"Get ID of parent category"},"content":{"rendered":"<h4>The problem<\/h4>\n<p>Sometimes you need to get the ID of the parent category when you&#8217;re browsing a child category page. Don&#8217;t you? \ud83d\ude42<!--more--><\/p>\n<h4>The solution<\/h4>\n<p>Get the ID of the parent category with a few lines of PHP code:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">&lt;?php\r\nif( is_category() ) {\r\n  $q_cat = get_query_var('cat');\r\n  $cat = get_category( $q_cat );\r\n  echo $cat-&gt;category_parent; \/\/ Print the ID\r\n}\r\n?&gt;<\/pre>\n<h4>Requirements<\/h4>\n<ul>\n<li>WordPress 1.5.1 and above<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The problem Sometimes you need to get the ID of the parent category when you&#8217;re browsing a child category page. Don&#8217;t you? \ud83d\ude42<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[17,19,18,16],"class_list":["post-49","post","type-post","status-publish","format-standard","hentry","category-wordpress-hacks","tag-category","tag-child","tag-id","tag-parent"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Get ID of parent category &#8211; wpseek.com \/\/ blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/wpseek.com\/blog\/2009\/get-id-of-parent-category\/49\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Get ID of parent category &#8211; wpseek.com \/\/ blog\" \/>\n<meta property=\"og:description\" content=\"The problem Sometimes you need to get the ID of the parent category when you&#8217;re browsing a child category page. Don&#8217;t you? \ud83d\ude42\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wpseek.com\/blog\/2009\/get-id-of-parent-category\/49\/\" \/>\n<meta property=\"og:site_name\" content=\"wpseek.com \/\/ blog\" \/>\n<meta property=\"article:published_time\" content=\"2009-11-13T16:59:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-07T08:40:20+00:00\" \/>\n<meta name=\"author\" content=\"wpseek (admin)\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"wpseek (admin)\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/2009\\\/get-id-of-parent-category\\\/49\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/2009\\\/get-id-of-parent-category\\\/49\\\/\"},\"author\":{\"name\":\"wpseek (admin)\",\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/#\\\/schema\\\/person\\\/d19081ecaffc56ccc034dbd009ede8eb\"},\"headline\":\"Get ID of parent category\",\"datePublished\":\"2009-11-13T16:59:44+00:00\",\"dateModified\":\"2022-03-07T08:40:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/2009\\\/get-id-of-parent-category\\\/49\\\/\"},\"wordCount\":76,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/#\\\/schema\\\/person\\\/d19081ecaffc56ccc034dbd009ede8eb\"},\"keywords\":[\"category\",\"child\",\"ID\",\"parent\"],\"articleSection\":[\"WordPress Hacks\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/wpseek.com\\\/blog\\\/2009\\\/get-id-of-parent-category\\\/49\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/2009\\\/get-id-of-parent-category\\\/49\\\/\",\"url\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/2009\\\/get-id-of-parent-category\\\/49\\\/\",\"name\":\"Get ID of parent category &#8211; wpseek.com \\\/\\\/ blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/#website\"},\"datePublished\":\"2009-11-13T16:59:44+00:00\",\"dateModified\":\"2022-03-07T08:40:20+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/2009\\\/get-id-of-parent-category\\\/49\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/wpseek.com\\\/blog\\\/2009\\\/get-id-of-parent-category\\\/49\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/2009\\\/get-id-of-parent-category\\\/49\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Get ID of parent category\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/\",\"name\":\"wpseek.com \\\/\\\/ blog\",\"description\":\"A random blog about WordPress\",\"publisher\":{\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/#\\\/schema\\\/person\\\/d19081ecaffc56ccc034dbd009ede8eb\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/#\\\/schema\\\/person\\\/d19081ecaffc56ccc034dbd009ede8eb\",\"name\":\"wpseek (admin)\",\"logo\":{\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"http:\\\/\\\/wpseek.com\"],\"url\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/author\\\/wpseek\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Get ID of parent category &#8211; wpseek.com \/\/ blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/wpseek.com\/blog\/2009\/get-id-of-parent-category\/49\/","og_locale":"en_US","og_type":"article","og_title":"Get ID of parent category &#8211; wpseek.com \/\/ blog","og_description":"The problem Sometimes you need to get the ID of the parent category when you&#8217;re browsing a child category page. Don&#8217;t you? \ud83d\ude42","og_url":"https:\/\/wpseek.com\/blog\/2009\/get-id-of-parent-category\/49\/","og_site_name":"wpseek.com \/\/ blog","article_published_time":"2009-11-13T16:59:44+00:00","article_modified_time":"2022-03-07T08:40:20+00:00","author":"wpseek (admin)","twitter_card":"summary_large_image","twitter_misc":{"Written by":"wpseek (admin)"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/wpseek.com\/blog\/2009\/get-id-of-parent-category\/49\/#article","isPartOf":{"@id":"https:\/\/wpseek.com\/blog\/2009\/get-id-of-parent-category\/49\/"},"author":{"name":"wpseek (admin)","@id":"https:\/\/wpseek.com\/blog\/#\/schema\/person\/d19081ecaffc56ccc034dbd009ede8eb"},"headline":"Get ID of parent category","datePublished":"2009-11-13T16:59:44+00:00","dateModified":"2022-03-07T08:40:20+00:00","mainEntityOfPage":{"@id":"https:\/\/wpseek.com\/blog\/2009\/get-id-of-parent-category\/49\/"},"wordCount":76,"commentCount":0,"publisher":{"@id":"https:\/\/wpseek.com\/blog\/#\/schema\/person\/d19081ecaffc56ccc034dbd009ede8eb"},"keywords":["category","child","ID","parent"],"articleSection":["WordPress Hacks"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/wpseek.com\/blog\/2009\/get-id-of-parent-category\/49\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/wpseek.com\/blog\/2009\/get-id-of-parent-category\/49\/","url":"https:\/\/wpseek.com\/blog\/2009\/get-id-of-parent-category\/49\/","name":"Get ID of parent category &#8211; wpseek.com \/\/ blog","isPartOf":{"@id":"https:\/\/wpseek.com\/blog\/#website"},"datePublished":"2009-11-13T16:59:44+00:00","dateModified":"2022-03-07T08:40:20+00:00","breadcrumb":{"@id":"https:\/\/wpseek.com\/blog\/2009\/get-id-of-parent-category\/49\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wpseek.com\/blog\/2009\/get-id-of-parent-category\/49\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/wpseek.com\/blog\/2009\/get-id-of-parent-category\/49\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/wpseek.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Get ID of parent category"}]},{"@type":"WebSite","@id":"https:\/\/wpseek.com\/blog\/#website","url":"https:\/\/wpseek.com\/blog\/","name":"wpseek.com \/\/ blog","description":"A random blog about WordPress","publisher":{"@id":"https:\/\/wpseek.com\/blog\/#\/schema\/person\/d19081ecaffc56ccc034dbd009ede8eb"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/wpseek.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/wpseek.com\/blog\/#\/schema\/person\/d19081ecaffc56ccc034dbd009ede8eb","name":"wpseek (admin)","logo":{"@id":"https:\/\/wpseek.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/wpseek.com"],"url":"https:\/\/wpseek.com\/blog\/author\/wpseek\/"}]}},"_links":{"self":[{"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/posts\/49","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/comments?post=49"}],"version-history":[{"count":2,"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/posts\/49\/revisions"}],"predecessor-version":[{"id":12490,"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/posts\/49\/revisions\/12490"}],"wp:attachment":[{"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/media?parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/categories?post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/tags?post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}