17

Are posts supposed to have parents? If so, what would that mean for a post to have a parent?

Also, if there are some constraints to posts having parents, then where is that enforced? Not in the DB as I see it.

5 Answers 5

16

Out of the box, no, posts can't have parents. They can be assigned to categories, which can be organised hierarchically. Pages, however, can have parents and you can build a menu structure out of them by using that feature.

As to where this is enforced: The parent of a post is stored in the column "post_parent" in "wp_posts". It's not really enforced as such, just the default UI doesn't give you an option to set it and default WP coding doesn't use that value for Posts. It wouldn't be too difficult to create a custom post type to have posts with parents though.

4
  • 2
    Also note that media attachments make use of the post_parent column, to identify which page/post (including custom post types) that they are attached to.
    – David Gard
    Apr 2, 2014 at 11:40
  • @DavidGard You are right. But I think, that functionality won't affect if we assign post_parent to an another "post_id" as it would be 0 otherwise.
    – Stranger
    Feb 1, 2017 at 12:23
  • what if I want post 1 and post 2 to have to different parents (which are pages)
    – magnus
    Aug 29 at 11:29
  • @magnus I converted this to a comment and put it here, but I don't know who you were trying to reply to, the order things appear is based on the number of votes not the time, remember this is not a discussion forum/messageboard, and if you have a question it's best asked by clicking the ask question button in the top right. Leaving questions in the answer box will get it flagged for moderation and you could be flagged as a spambot
    – Tom J Nowell
    Aug 29 at 14:06
16

Wp has built in "Pages" (hierarchical, parents allowed) and "Posts" (non-hierarchical). There are also other post types, but let's leave that away.

If the Q results in:

Can I have hierarchical posts?

Then the answer is Yes,… you can have "posts" that are hierarchical. But as they are not built in, you'll have to register your own Custom Post Type - see Arguments » hierarchical.

Such "Posts" (or articles, whatever, …) will then - in case they have a parent post - have set the parent ID inside their object. So in a loop you could do the following:

if ( have_posts )
{
    the_post();
    // etc.

    global $post;
    // call parent: http://codex.wordpress.org/Function_Reference/get_post
    $parent = get_post( $post->post_parent );

    echo "<h2>{$post->post_title} is a child of {$parent->post_title}";

    // etc.
} // endif;
1
  • 2
    Nailed it perfectly. Exactly the thing I was looking for.
    – Paras Shah
    Jan 3, 2017 at 11:29
-1

From a search engine perspective, two of these answers combined will help Google to track the hierarchy of your posts, as well as users of course.

  1. Add a page and use it to host your posts. "The best way to set a parent page through all of your posts is through the (Appearance -> Customize) menu. You can set a static page for your home page or posts page. When you set a static post page, that page will be the parent for every individual blog post."

  2. Change the permalink structure to /blog/%postname% as mentioned above. "Suppose you want to append blog before post in url than make sure that your blog page's slug name is blog .

Then go to WP-admin-panel > settings > permalink and choose the last option "custom structure" and add following in it:

/blog/%postname%/

and save changes. It will be only applied to your blog page, other page will not display blog as parent."

This way, Google sees the hierarchy in sitemaps as domain.com/page/post (in that order) and will also find the content in the same place. As will users. Job done.

-4

Suppose you want to append blog before post in url than make sure that your blog page's slug name is blog .

Then go to WP-admin-panel > settings > permalink and choose the last option "custom structure" and add following in it:

/blog/%postname%/

and save changes.

It will be only applied to your blog page, other page will not display blog as parent.

-6

I know this is an old post. However none of these answers are correct, and I wanted to post the solution I used to solve this problem. The best way to set a parent page through all of your posts is through the (Appearance -> Customize) menu. You can set a static page for your home page or posts page. When you set a static post page, that page will be the parent for every individual blog post.

4
  • 2
    That's not a parent page - it's a template containing an archive of posts. Setting that up doesn't make posts hierarchical.
    – kaiser
    Apr 2, 2014 at 11:24
  • How is that not a parent page? It's a static page (which by the way does not have to be an archive, whatever page template you assign to that static page, can have whatever you want), and when this is set up your urls follow that hierarchy, as well as Breadcrumbs on the site. It's a 1 minute fix that allows you to assign a static page as the post parent. His question mentions NOTHING about it being hierarchical in the first place. Perhaps you could enlighten me on how this solution would be bad practice? Apr 6, 2014 at 15:11
  • "Are posts supposed to have parent" is the same thing as "Are posts hierarchical". A template can contain everything, but the plain fact that it contains a loop that returns and displays an archive loop does not (a) make the page its parent (it's just the place where the loop happens to be) and (b) does not make the built in post type "post" hierarchical or in other words, does not assign a parent. But yes, you can summon posts in a loop by specific criteria (like a shared child-category).
    – kaiser
    Apr 6, 2014 at 16:19
  • Don't get thrown off by criticism or clarification of your question or answer. This place is something where this actually needs to happen all the time to make the best answers succeed and serve as wiki/library place where one can go and instantly figure out how to do something or get as much possible insights on how something works. We even encourage people (like you) to comment on other answers as well. Keep in mind that I nowhere mentioned that your answer is bad practice. It's true in a lot of ways, just the statement that it makes the post type hierarchical isn't.
    – kaiser
    Apr 6, 2014 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.