Rewrite numeric ID parameter for hierarchical custom post type

I’ve been banging my head against a wall on this one for a while but I’m not quite sure from all the tutorials I read how to see why this isn’t doing what I expect.

I have a page called shop-guide/shop-page?id=412 (for example)

Read More

and I want to change it so it it’s shop-guide/shop-page/id/412/

As far as I can tell I have my code set up correctly, but when I try to navigate to the page I get a 404, and if I use the $_GET version it doesn’t rewrite.

Here’s my code: (in functions.php)

function wptuts_add_rewrite_rules() {  
add_rewrite_rule(  
    'shop-guide/shop-page/id/([^/]*)/',  
    'index.php?p=143&id=$matches[1]',  
    'top' 
);  
}  
add_action( 'init', 'wptuts_add_rewrite_rules' ); 

Related posts

Leave a Reply

2 comments

  1. Two issues –

    your rewrite rule isn’t formatted correctly, this:

    'shop-guide/shop-page/id/([^/]*)/'
    

    should be:

    'shop-guide/shop-page/id/([^/]*)/?$'
    

    and if shop-guide and shop-page are parent / child pages, this:

    'index.php?p=143&id=$matches[1]'
    

    should be:

    'index.php?pagename=shop-guide/shop-page&id=$matches[1]'