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.

Wondering if someone could tell me why sub pages (child pages) no longer work (eg: www.mysite.com/type/childpage).

I have a page called "Type" (working fine), and I wish to have Child Pages of "Type" (404 errors)- as you can see I also like to have the "type" rewrite slug in front of my custom post types/taxonomies.

I tried the method from this link to no avail: http://wordpress.stackexchange.com/questions/4127/custom-taxonomy-and-pages-rewrite-slug-conflict-gives-404

add_action( 'init', 'create_zombie_type' );

function create_zombie_type() {
register_post_type('zombies', 
array(  
'description' => 'Zombie custom post type',
'show_ui' => true,
'menu_position' => 5,
'menu_icon' => get_stylesheet_directory_uri() . '/images/zombies.png',
'exclude_from_search' => false,
'labels' => array(
    'name' => 'Zombies',
    'singular_name' => 'Zombie',
    'add_new' => 'Add New Zombie',
    'add_new_item' => 'Add New Zombie',
    'edit' => 'Edit Zombies',
    'edit_item' => 'Edit Zombie',
    'new_item' => 'New Zombie',
    'view' => 'View Zombie',
    'view_item' => 'View Zombie',
    'search_items' => 'Search Zombies',
    'not_found' => 'No zombies found',
    'not_found_in_trash' => 'No zombies found in Trash',
    'parent' => 'Parent Zombie',
),
'hierarchical' => false,
'supports' => array('title','editor','excerpt', 'trackbacks','custom-fields',     'comments','revisions','thumbnail','author','page-attributes'),
'public' => true,
'rewrite' => array('slug' => 'type/%freshness%', 'with_front' => false),
'taxonomies' => array('freshness')
) 
);
}




add_action( 'init', 'create_zombie_taxonomies' );

function create_zombie_taxonomies() {
register_taxonomy( 
'freshness', 
'zombies',
array( 
'labels' => array(
    'name' => 'Freshness',
    'singular_name' => 'Freshness',
    'search_items' =>  'Search Freshness',
    'popular_items' => 'Popular Freshness',
    'all_items' => 'All Freshness',
    'parent_item' => 'Parent Freshness',
    'parent_item_colon' => 'Parent Freshness:',
    'edit_item' => 'Edit Freshness', 
    'update_item' => 'Update Freshness',
    'add_new_item' => 'Add New Freshness',
    'new_item_name' => 'New Freshness Name'
), 
'hierarchical' => true, 
'public' => true,
'show_ui' => true,
'query_var' => 'freshness',
'show_tagcloud' => true,
'rewrite' => array( 'slug' => 'type', 'with_front' => false ) 
) 
   );

}



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

if ($cats = get_the_terms($post->ID, 'freshness'))
$link = str_replace('%freshness%', array_pop($cats)->slug, $link);
return $link;
}

add_filter('post_type_link', 'filter_post_type_link', 10, 2);
share|improve this question
1  
Actually got this to work with the link I provided up top--- is there any way to add the filter to any subpage created in the directory instead of having to specify each one? –  Joe May 10 '11 at 23:49

1 Answer 1

You could create verbose page rules for each page that starts with /type/, but you will also need to hook into page creating and stuff like that to keep it up-to-date. If you don't have that many extra pages, it might be easier to just force verbose page rules for all pages, then WordPress will do this for you.

add_action( 'init', 'wpse16902_init' );
function wpse16902_init() {
    $GLOBALS['wp_rewrite']->use_verbose_page_rules = true;
}

The only problem now is that the taxonomy rules are put even on top of that (since WP 3.1 I think). But with the correct hooks we can intercept the page rules and place them really at the top.

add_filter( 'page_rewrite_rules', 'wpse16902_collect_page_rewrite_rules' );
function wpse16902_collect_page_rewrite_rules( $page_rewrite_rules )
{
    $GLOBALS['wpse16902_page_rewrite_rules'] = $page_rewrite_rules;
    return array();
}

add_filter( 'rewrite_rules_array', 'wspe16902_prepend_page_rewrite_rules' );
function wspe16902_prepend_page_rewrite_rules( $rewrite_rules )
{
    return $GLOBALS['wpse16902_page_rewrite_rules'] + $rewrite_rules;
}

It is possible that the rewrite rules for the custom post type and the custom taxonomy collide with each other for the /type/%freshness%/ URLs. You want these to be a taxonomy archive, not a custom post type archive. If you have problems with this, reverse the order in which you register them: first the taxonomy, then the custom post type:

register_taxonomy(
    'freshness',
     array(), // Empty array, no post types linked
     array(
         'rewrite' => array(
             'slug' => 'type',
         )
     ),
 );
 register_post_type(
     'zombie',
     array(
         'rewrite' => array(
             'slug' => 'type/%freshness%',
         ),
         'taxonomies' => array( 'freshness' ),
     ),
 );

Extra notice: if your taxonomy URLs really start with /type/, you might get into conflict with post format archives. /type/aside/ by default displays all posts of the "Aside" format. This answer assumes the zombie custom post type and taxonomy is just an example, and you use another URL format in your real problem.

share|improve this answer
    
For those (like me) who don't know, you need to flush the rewrite rules. –  Blang Mar 22 '12 at 5:47

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.