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
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!
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:
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
Have you tried flushing your rewrite rules after add_rewrite_rule():
This isn’t something you’d leave in production but it could determine if that rule is being added to your rewrites or not. You can also confirm afterwards with:
to see if your rule was added.
I was having a very similar issue and was perplexed that the re-write analyzer showed a match on my rewrite rule but the page was doing a strange redirect. Once I read this article: http://www.dev4press.com/2012/tutorials/wordpress/practical/how-wordpress-url-rewriting-works/ I realized that the post I was trying to rewrite to in my rewrite rule was a page which is a “Custom Post Type” and therefore wordpress does not support url rewrites for that. The odd redirect is because of the “Canonical Redirect” mentioned at the bottom of that article. I tried rewriting to a “post” not a “page” and it worked.
From the documentation
I believe WordPress is not recognizing the “shareholder_name”
You need to do this
Are you sure your custom post type has a separate single.php file?
I was having a problem and this was the situation:
I have three custom post types. I recently created the new one and its permalink redirects me to a diff custom post type’s page. I have tried all rewrite flush rules etc but then I came to realize that the problem was because I don’t have a separate single.php file for my new custom post type, resulting to my new post type having a redirect to an existing post of different type by default.
Just look into it, maybe you have the same situation as mine. Goodluck!
I miss a flush() command. And I got the expression (painfully, over hours) that it’s best to stay away from custom_rewrite, at least under word 3.1
Have a look at my answer to (well, my) question here:
flexible rewrite ‘ramble’ URLs with WordPress
It could be your 4-line solution in two files ๐