Question:

URL Rewrite + Page + Custom Post Type = Unusual Redirect

Ellie: 5 days ago

Yet another URL rewrite issue :)

The Problem

I have a URL rewrite rule that should work, but against all logic it redirects without passing the query var.

The Juicy Details

The site has a page structure set up like so:

/our-firm/shareholders

and I have a Custom Post Type named "shareholder." My goal is to have the structure work like this:

/our-firm/shareholders/awesome-shareholder-name

...and for the Shareholders page to do a custom query for the Shareholder posts. I normally would use the rewrite slug for the Shareholder posts, but I want the content to be shown in the context of the original Page structure (our-firm/shareholders).

In order to achieve that, I added this code in functions.php (and have since gone to Permalinks and saved to refresh the rules):

add_action( 'init', 'my_custom_rewrites' );
function my_custom_rewrites(){
    add_rewrite_rule(
        'our-firm/shareholders/([^/]+)/?',
        'index.php?pagename=shareholders&shareholder_name=$matches[1]',
        'top' );
}

add_filter( 'query_vars', 'my_custom_query_vars' );
function my_custom_query_vars( $query_vars ){
    $query_vars[] = 'shareholder_name';
    return $query_vars;
}

All that in place, if I go to /our-firm/shareholders/awesome-shareholder-name it redirects me to the Shareholders page without passing the query var and actually changes the URL to /our-firm/shareholders/. The weird part is if I change the redirect pagename to anything else, it works and effortlessly passes the query var along.

To further test it, I used the handy Rewrite Analyzer plugin and got this:

Rewrite Analyzer Test Screenshot (http://cl.ly/95Oc)

Additional Notes

  • I am running WP 3.2.1, testing locally, and have not fiddled with .htaccess

  • My Shareholders Page template isn't doing anything unusual, but let me know if you need to see it

  • Does Wordpress take into account the label of a Custom Post Type when figuring out URLs? The plural label of "shareholder" is, naturally, "Shareholders."

  • I am adding the "shareholder" CPT via the Custom Post Type UI plugin. I can manually add it with code if anyone feels that would help. These are the CPT settings:

    rewrite = true, slug = 'shareholder', with_front = true, query var = true, and hierarchical = true.

  • I previously had the "shareholder" post type ID as "shareholders," then quickly realized that would cause issues being the same name as the page I was attempting to redirect to. I then changed the post type to "shareholder," which unfortunately didn't resolve the issue. Could the fact that there one was a post type of "shareholders" be affecting this? Maybe there's a secret magical cache somewhere that I could clear out...

  • I know there are at least 5 bajillion WP URL rewrite posts across the web, but I can't seem to find one that pertains to my specific issue. Sorry if I missed one :)

I'm at my wit's end – I know that this should work, but it doesn't. Hopefully I missed something painfully obvious and it's an easy fix. Please let me know if I can provide any more details/code/etc.

Any help would be appreciated, thanks!

Answer:
Daniel: 5 days ago

You should try setting 'slug' ( in the 'rewrite' parameter/option) to 'our-firm/shareholders', just like the structure you have set in the add_rewrite_rule() function.

The first parameter of add_rewrite_rule() and the 'rewrite' setting of the custom post type must be equal in order to get the rewritting actually working.

So, in case you choose coding the registration of the custom post type, your register_post_type() should look something like this:

$args = array( 'rewrite' => array( 'our-firm/shareholders', 'with_front' => true ) );
register_post_type( 'shareholder', $args );

Of course you need to fill the $args array with some other keys. I've just specified 'rewrite' to keep the explanation simple. Follow the documentation on this function to craft a proper registration: http://codex.wordpress.org/Function_Reference/register_post_type

If changes are not reflected, then try flushing the rewrite rules. You can accomplish this in a safety way by clicking on "Save Changes" in Admin Page > Settings > Permalinks