Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 = '/', $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, but id love some help with find out what thats supposed to be. Thanks!

share|improve this question

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/

?

share|improve this answer
    
Well actually this code ended up working: pastie.org/private/73fxmsa1afzxnnpsfnfp6q, but for some reason no matter what, the flush rules happens and if I disable that action, the rules dont seem to apply and I get 404's again. =/ Flushing the rules should only need to be done once. No idea why they arent becoming permanent. –  Mark Chaney Jun 22 '12 at 8:02

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.