4

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!

2
  • WordPress automatically redirects to whatever the permalink is. Go to the edit page of one of that post type and see if it matches what the url is redirected to. Commented Aug 7, 2011 at 21:27
  • @manny-fleurmond The post type's slug is "shareholder," so all of the Shareholder posts have this structure for the permalink: /shareholder/post-title/. I specifically left it that way so it wouldn't interfere with the intended /our-firm/shareholders/person-name/ structure. Commented Aug 7, 2011 at 23:07

6 Answers 6

3

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

0

Have you tried flushing your rewrite rules after add_rewrite_rule():

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

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:

global $wp_rewrite;
var_dump($wp_rewrite->rules);

to see if your rule was added.

2
  • Hey, I know you! ;) Thanks for the reply – I tried both of those things and it does shows up in the list of rules, but it unfortunately doesn't solve the issue. Putting anything after /our-firm/shareholders/ redirects it to the Shareholders Page and removes the extra bit from the URL Commented Aug 11, 2011 at 4:34
  • I just looked into the plugin you are using a bit more. Try setting the "Custom Rewrite Slug" field to "our-firm/shareholders" and removing anything you have in your functions.php file as mentioned above. I think that should make the custom post types stay within the 'our-firm' directory.
    – Josh
    Commented Aug 11, 2011 at 12:56
0

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.

1
  • WordPress allows url rewrites for custom post types... Commented Sep 27, 2012 at 9:29
0

From the documentation

IMPORTANT: By default, WordPress will not recognize custom querystring variables used for rewrites. You must register your querystring variables with WordPress. Simply use add_rewrite_tag() to capture querystring variable values after a rewrite, or the above rewrite will not work!

I believe WordPress is not recognizing the "shareholder_name"

You need to do this

add_action( 'init', 'my_custom_rewrites' );
function my_custom_rewrites(){

    add_rewrite_tag( '%shareholder_name%', '([^/]+)' );

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

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!

-1

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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.