I’m trying to create sub-posts like this:
/post/info/
/post2/info/
Using wp_insert_post() and I have almost succeeded. I’m setting post_parent to the parent post ID and the post_type needs to be “attachment”.
But! The problem is that the post_name needs to be unique so the sub page needs to be info, info2 etc and that’s not what I want. So when settings the post_name to the same I’m getting all sub pages on one page when visiting /post/info/ and /post2/info/.
Anyone knows if it’s possible to accomplish this?
Hi @jonasl:
I asked a clarifying question but I’m going to go ahead and at least start to answer.
The function in WordPress core that controls post slugs and adds numbers to them in order to force them to be unique is
wp_unique_post_slug()
. In WordPress 3.0.3 you can find it on line 2530 in/wp-includes/post.php
. I’ve copied it in full below for your review.You’ll note it says “Attachment slugs must be unique across all types” so if your children are
post_type='attachments'
then this is what is forcing them to be unique. In addition you’ll note that the post types must have'hierarchical'=>true
or it will force them to be unique across all posts.If you are not using attachments as children (which I fear you are based on your question) then you can possibly just define the post type as being
'hierarchical'=>true
. If you can’t do that I might be able to suggest a have to fake it during insert of the post but I can’t promise doing so will not cause other posts in WordPress to break.If you are using attachments and need this maybe you can explain more what you are trying to accomplish so that maybe we can suggest an alternative.