Parent from different post type

  1. mralexweber

    Just found this page http://janina.tumblr.com/post/3588081423/post-parent-different-type
    which shows that it is possible, and rather easy (if you are comfortable with PHP)

    Posted: 4 years ago #
  2. Georgi Popov


    I believe various ways to actually assign a parent entry from a different post type were already proposed but the main issue remains unsolved -- broken permalinks.

    Posted: 4 years ago #
  3. FranH

    I totally agree with this! just had to resolve the same issue myself.

    I have a "Gallery" and "Image" post types, and naturally images should be children of galleries.

    First step is to set the same rewrite_slug, in my case /galleries.
    Second, I added a rewrite rule to handle the images properly. Warning: not a pretty sight! :)

    function gallery_image_rewrite_rules($rules) {
        $matches = array();
        preg_match_all( '!^/galleries/[^/]+/([^/]+)!', $_SERVER['REQUEST_URI'], $matches );
        $slug = @$matches[1][0];
        if ( ! empty( $slug ) ) {
            $image_query = new WP_Query(array(
                'post_type' => 'image',
                'name' => sanitize_title($slug),
            $image_posts = $image_query->get_posts();
            if ( count( $image_posts ) ) {
                $newRules  = array();
                $newRules['galleries/(.+)/(.+)/?$'] = 'index.php?post_type=image&p='.$image_posts[0]->ID;
                $rules = array_merge($newRules, $rules);
        return $rules;
    add_filter('rewrite_rules_array', 'gallery_image_rewrite_rules');

    That allows me to have a gallery at example.com/galleries/somegallery-slug/ and images at example.com/galleries/somegallery-slug/someimage-slug/

    Disclamer: This is both not pretty and not tested very much.

    Posted: 4 years ago #

RSS feed for this topic


You must log in to post.

  • Rating

    7 Votes
  • Status

    This is plugin territory