Custom post types and permalinks

I have a custom post type called Content, that has a capability type of ‘page’ and is set to 'hierarchical' => true. I have two pages with the same name “Children”, but they have different parents. What should i be doing differently so that WP doesnt display both of them at the same time no matter which of the two urls i go to? Here is a snippet of my code that displays the rewrite rules:

add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
    function mmp_rewrite_rules($rules) {
        $newRules  = array();
        $newRules['content/(.+)/(.+)/?$'] = 'index.php?content=$matches[2]'; 
        $newRules['content/(.+)/?$']      = 'index.php?campus=$matches[1]'; 

        return array_merge($newRules, $rules);
    }

    function filter_post_type_link($link, $post)
    {
        if ($post->post_type != 'content')
            return $link;

        if ($cats = get_the_terms($post->ID, 'campus'))
        {
            $link = str_replace('%campus%', get_taxonomy_parents(array_pop($cats)->term_id, 'campus', false, '', true), $link); // see custom function defined below
        }
        return $link;
    }
    add_filter('post_type_link', 'filter_post_type_link', 10, 2);

    // my own function to do what get_category_parents does for other taxonomies
    function get_taxonomy_parents($id, $taxonomy, $link = false, $separator="https://wordpress.stackexchange.com/", $nicename = false, $visited = array()) {    
        $chain = '';   
        $parent = &get_term($id, $taxonomy);

        if (is_wp_error($parent)) {
            return $parent;
        }

        if ($nicename)    
            $name = $parent -> slug;        
    else    
            $name = $parent -> name;

        if ($parent -> parent && ($parent -> parent != $parent -> term_id) && !in_array($parent -> parent, $visited)) {    
            $visited[] = $parent -> parent;    
            $chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $separator, $nicename, $visited);

        }

        if ($link) {
            // nothing, can't get this working :(
        } else    
            $chain .= $name . $separator;    
        return $chain;    
    }

The two urls are like so:

/content/peoria/new-visitor/children/

/content/peoria/connect/next-generation/children/

When going to either one of those urls, it should only be the content of that particular ‘page’ and not the contents of both on the same page.

Any suggestions? Im assuming the $newRules area needs to be redone.

1 Answer
1

I’m not really sure whats going on here – havent done too much with rewrites like this.

But should this:

$newRules['content/(.+)/(.+)/?$'] = 'index.php?content=$matches[2]'; 
$newRules['content/(.+)/?$']      = 'index.php?campus=$matches[1]'; 

Be This?:

$newRules['content/(.+)/(.+)/(.+)/?$'] = 'index.php?content=$matches[3]'; 
$newRules['content/(.+)/(.+)/?$']      = 'index.php?campus=$matches[2]'; 

To Match:

/content/peoria/new-visitor/children/   
/content/peoria/connect/next-generation/children/

?

Leave a Comment