I am creating a web-site on wordpress platform where I want to be able to post my own book texts. So what I want is to have a some kind of hierarchy where I would add a post and then add children to it (chapters). I found this:
register_post_type( 'post', array(
'labels' => array(
'name_admin_bar' => _x( 'Post', 'add new on admin bar' ),
),
'public' => true,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
) );
and tried to make the 'hierarchical"=>true
, but there was no effect. Can anyone help?
Here is my workaround. This achieves exactly what you want, to be able to set post parents for the builtin post type post. You can achieve this by adding an action to the
registred_post_type
action hook. Just add this to your theme’s functions.php.There can be dozens of reasons why making posts hierarchical can be helpful. My use case is that the client wanted to structure their (already existing) posts into issues, where child posts are articles of one issue (parent posts).
This is easily achieved by limiting the query to only show posts that have no parents, using.
in your query $args.
WP 4.9.*
Workaround above makes it crazy with Friendly URLs.
My solution to add hierarchy to any existent post type:
Resave wp settings at /wp-admin/options-permalink.php
Update
Due to comments provoking new use-cases and issues, I rewrote this code and I am using it on my own sites [tested in 5.8.2]. I have provided a gist for it. You can include it in your functions.php, or make it into a plugin.
ð This new update is leveraging SQL (fast!) to resolve the slug and post id to determine the permalink & routing. It produces the exact matching post id, even if you are using the same
post_name
for different post descendants. It’s really fast & reliable!In the gist, the most interesting function is
get_post_from_uri($uri)
ð Let’s see how the query works. This may not be a perfect 1-1 of the code, because I made it dynamic, but the concept is there:
Example:
I have the following posts:
See it in SQL:
Ex URL:
alliance-for-innovation/climate
The full query…
I now have both the post
ID
and theslug
, or permalink, I should be using!It is worth noting I went to the level of p3, which is one extra level than the URL would require (being two parts). This is to prevent something like
alliance-for-innovation/climate/something
from matching.How does it work? Break down the query
There’s an inside query that looks for the last part of the URL, aka basename. In this case it would be
climate
.Programmatically, we keep adding abstractions around the query that’s directly related to the number of
/
in the url, so that we can find more information about the post_parent’s slug.After we abstracted enough times, we can then select concats as slug like: p1_slug + ‘/’ + p2_slug
The last step is to add a
where
for the original url:alliance-for-innovation/climate
. And that’s what you see in the full query example we first examined!Let’s see how the others go:
Another thing that I like about this update is that I remembered to:
Escape the
climate
, or basename of the URL that we use in the query, because this is technically user-inputted (via url)So how is this query-building function dynamic?
We use PHP arrays, loops, etc to build a string that will be the query so that we do not have to use PHP for logic about the data itself.
This is a snippet showing the dynamic abstractions – eg. how many p1_slug, p2_slug, p3_slug to grab.
Previous Answer:
I came here looking to achieve:
I was able to use the accepted answer to accomplish 1 & 2, but not 3.
Note: to fully get 2 to work, you need to specify the post_type in the template comments of your page template like this:
For 3, I found a plugin that ruined my post_type pages, and it was a lot of pretty awful, unmaintained code.
So I wrote a solution to accomplish all this, borrowing from this answer:
(Tested with 4.9.8)
You can save this to a file
custom-posts-hierarchy.php
and include it in your functions.php file in your theme, or you can add to the top:And drop it into your plugins folder. Good luck!
Posts in WordPress are supposed to be typical chronological Blog Posts. Pages are made for static content, they can be organised in a hierarchical structure out of the box.
For any Page, you can select a parent page. This way, you can create nested hierarchies with multiple children. Sounds like what you need.
Check the WordPress Documentation for details.
If you have a deep, complicated tree structure, a plugin might help you manage it, like WordPress Page Tree. It provides a better interface than the default WordPress Page listing.
Using a plugins like
CPT UI
, you can create a custom post type and set it to have hierarchical tree.Then just check that the post type
page-attribute
is set for this custom post type and voile, your posts now have hierarchical states.https://wordpress.org/plugins/custom-post-type-ui/
best solution is to create custom taxonomy [1]: http://codex.wordpress.org/Function_Reference/register_taxonomy and create main slug – books or something else.
There exists plugin, which creates hierarchy for
post
:https://wordpress.org/plugins/add-hierarchy-parent-to-post/
Isn’t this a better option?
I have added:
‘hierarchical’ => true,
and it works.
I was also considering to add hierarchy to posts and I read through this topic. My conclusion is that (in my opinion) it is better to avoid adding hierarchy to posts. Post and page hierarchy is such a core concept of WordPress, and all kinds of issues with SEO and compatibility with plugins (and WordPress core) can come up when touching this.
Instead of implementing a complicated technical solution, and having to hope that it remains compatible with future WordPress and plugin updates, it seems better to use categories and subcategories for hierarchy of posts. And if that doesn’t provide sufficient flexibility, use pages instead of posts because pages also come with support for hierarchy out of the box (as previously mentioned by others).
There may be situations where it looks like post categories or pages both can’t solve your hierarchy problem but I’d still argue that you should consider whether you want to complicate things for yourself by implementing some form of customized post hierarchy solution, and all the future problems that can come from it.
While it is tempting to come up with a technical solution for a problem, sometimes the best answer is to not do something at all and use a different approach altogether. I strongly believe that is the case here.