Setting Parent Page to Post

Does anyone know how to achieve different parent pages for posts?

I've managed to code a Custom Post Type that sits under a specific parent page, but I want the option to change this from post to post.

Example;

  • /page-1/custom-post-1/
  • /page-1/custom-post-2/
  • /page-2/custom-post-3/
  • /page-2/custom-post-4/

The final result would be URL structure like the above, but all posts sitting on one archive page.

I know by default this isn't possible as posts are non-hierarchical.

Thanks

Topic url-rewriting seo customization custom-post-types Wordpress

Category Web


This should be in a comment, but can't format much in there.

  1. Create a relationship between your CPT and Page. For example, create a custom taxonomy for your CPT and programatically add the pages as terms. Or, you can even create a meta field.

  2. Create a placeholder for the URL while registering the CPT. For example 'rewrite' => array( 'slug' => '%cpttag%'),.

  3. str_replace the placeholder with page slug you get from term/meta using post_type_link.

  4. Flush the permalinks (Just hitting Save on Permalinks Settings page will do it)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.