{"id":284,"date":"2020-12-19T20:06:39","date_gmt":"2020-12-19T20:06:39","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/http\/code.oxyset.com\/?p=284"},"modified":"2026-10-10T20:52:22","modified_gmt":"2026-10-10T20:52:22","slug":"add-hierarchy-to-posts","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/add-hierarchy-to-posts\/","title":{"rendered":"Make WordPress Posts Hierarchical with a Parent Option"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">WordPress pages can have a parent page, posts cannot. Sometimes you want that for posts or a custom post type too: a guide split into chapters, documentation with sections, or a parent post that lists its child posts.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This snippet makes WordPress posts hierarchical. The editor gets the <strong>Parent<\/strong> and <strong>Order<\/strong> fields that pages have, the admin list shows children indented under their parent, and you can query child posts with <code>post_parent<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/**\n * Make WordPress posts (and other post types) hierarchical, with a Parent option in the editor.\n *\/\n\n\/\/ ===== Config =====\nfunction dpc_hierarchical_post_types() {\n\t\/\/ Post types that get a parent. Custom post types: add their slug, e.g. 'docs'.\n\t$post_types = array( 'post' );\n\t\/\/ ===== End of config =====\n\n\treturn (array) apply_filters( 'dpc_hierarchical_post_types', $post_types );\n}\n\nadd_filter( 'register_post_type_args', 'dpc_hierarchical_post_type_args', 10, 2 );\n\nfunction dpc_hierarchical_post_type_args( $args, $post_type ) {\n\tif ( ! in_array( $post_type, dpc_hierarchical_post_types(), true ) ) {\n\t\treturn $args;\n\t}\n\n\t$args&#91;'hierarchical'&#93; = true;\n\n\t\/\/ No 'supports' given means WordPress's default: title and editor. false means none.\n\t$supports = $args&#91;'supports'&#93; ?? array( 'title', 'editor' );\n\tif ( ! is_array( $supports ) ) {\n\t\t$supports = array();\n\t}\n\tif ( ! in_array( 'page-attributes', $supports, true ) ) {\n\t\t$supports&#91;&#93; = 'page-attributes';\n\t}\n\t$args&#91;'supports'&#93; = $supports;\n\n\treturn $args;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Add it with Scripts Organizer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/downloads\/scripts-organizer\/\">Scripts Organizer<\/a> go to <strong>Code Blocks \u2192 Add New<\/strong> and give the block a name. Set <strong>Trigger location<\/strong> to <strong>Everywhere<\/strong>, tick <strong>PHP<\/strong> under <strong>Script location<\/strong>, paste the code into the PHP editor, edit the config and publish. Leave <strong>Action\/Hook<\/strong> empty.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Everywhere<\/strong> is needed: the filter must be in place before WordPress registers the post types on every request, in wp-admin, the REST API and the front end.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The same code also works unchanged in a small plugin or an mu-plugin.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>register_post_type_args<\/code> lets you change the arguments of any post type while it is registered, including core&#8217;s <code>post<\/code> and post types from other plugins.<\/li>\n\n\n<li><code>hierarchical =&gt; true<\/code> allows a <code>post_parent<\/code>. The <code>page-attributes<\/code> feature adds the <strong>Parent<\/strong> and <strong>Order<\/strong> fields to the editor&#8217;s sidebar.<\/li>\n\n\n<li>A custom post type registered without <code>supports<\/code> gets WordPress&#8217;s defaults (title and editor) plus <code>page-attributes<\/code>. The older version used <code>array_merge()<\/code> on a missing <code>supports<\/code> value, which throws a TypeError on PHP 8 and stops the whole page.<\/li>\n\n\n<li>The post type list sits in the config function, and other code can change it with the <code>dpc_hierarchical_post_types<\/code> filter.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Custom post types: add their slug to <code>$post_types<\/code>, for example <code>array( 'post', 'docs' )<\/code>.<\/li>\n\n\n<li>List child posts on the parent: use a Query Loop block, or <code>get_children( array( 'post_parent' =&gt; get_the_ID(), 'post_type' =&gt; 'post' ) )<\/code> in PHP.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Post permalinks stay flat. A child post keeps <code>\/child-post\/<\/code> (or your date-based structure), not <code>\/parent\/child\/<\/code> as pages do.<\/li>\n\n\n<li>Hierarchical post types are listed differently in wp-admin: sorted by title and grouped under their parent, not by date. On a site with thousands of posts the list loads all post IDs to build the tree, which is slow.<\/li>\n\n\n<li>Making <code>post<\/code> hierarchical is not an officially supported setup. Most themes and plugins handle it, but check RSS feeds, archives and SEO plugins after enabling it.<\/li>\n\n\n<li>Removing the snippet keeps the <code>post_parent<\/code> values in the database; they are just no longer shown.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Give WordPress posts or custom post types a Parent field like pages, with register_post_type_args, safe for post types registered without supports.<\/p>\n","protected":false},"author":1,"featured_media":46824,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Make WordPress Posts Hierarchical with a Parent Option","description":"Give WordPress posts or custom post types a Parent field like pages, with register_post_type_args, safe for post types registered without supports."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[3],"tags":[],"dpc_library":[204],"class_list":["post-284","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/284","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=284"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/284\/revisions"}],"predecessor-version":[{"id":46823,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/284\/revisions\/46823"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46824"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=284"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=284"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}