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.

I have an issue with Canonicalization where on Custom Post Types say for example:

/custom-post-type/{post_id}/fake-post-name/

is loading up but not redirecting to :

/custom-post-type/{post_id}/real-post-name/

It basically is allowing anything to be after post id and load that URL instead of redirecting to the correct one.

Ive got my CPTs setup with custom rewrite rules using 'add_rewrite_rule' and associated my custom post links with 'post_type_link'.

Have I missed a step somewhere?

Many thanks.

Just add to this the actual Canonical tag displays the correct url it should redirecting to.

Will I need to add a filter to canonical_redirect and check the post_name of the CPT and see if it matches for the given post id and pass it back perhaps? Im not sure if this is needed/or way to fix it given the canonical in the is correct?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Think ive solved it and can't believe what it was!

On my rewrite rule I had:

add_rewrite_rule( 
        'custom-post-type/([0-9]+)/([^/]*)/?$',
        'index.php?post_type=customposttype&p=$matches[1]',
        'top'
    );

Adding in and passing the post_name for the custom post type got it to start redirecting.

add_rewrite_rule( 
        'custom-post-type/([0-9]+)/([^/]*)/?$',
        'index.php?post_type=customposttype&p=$matches[1]&customposttype=$matches[2]',
        'top'
    );

Anycase for now its late and it seems to have started working :-) Now at least it forces the 301 from the fake post to the real post as was previously put in canonical meta. Don't know why but thought if the id was provided it would have worked without having to take in the post_name.

Sometimes WP can really have you pulling your hair out!

share|improve this answer
    
Moral of the story from this for me sleeping on it is if you include variables in the url string make sure all are passed in even if you don't believe Wordpress will use them for thoroughness then this should not occur. Hopefully this will help someone else in future. –  Jimbo Mar 16 '13 at 15:22

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.