Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers

up vote 3 down vote accepted

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.

share|improve this answer

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 ) : 
// 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;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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