Wordpress Custom Post Type infinite redirect loop using Advanced Permalinks
Using Wordpress 3.1 and the latest Advanced Permalinks and Custom Post Type UI i have created a custom post type called 'people'. The url pattern for all children of this post type are people/jim, however when i view post i get stuck in an infinite redirect loop. This only happens when I use pretty permalinks, not when id's are used.
The permalinks str开发者_开发问答ucture used on Advanced Permalinks are:
Common Settings
Custom Structure: %postname%
Post
%postname%
- Wordpress is redirecting the custom post type to itself instead of translating it as ?people=jim. I have tried defining the post types myself in functions.php and then doing a flush but that doesn't seem to fix the issue as others have found. Greatly appreciate any fix!
After a lot of logging and debugging, I found that the function that was causing the infinite redirect was function the_posts($posts)
. If you comment out everything from if (is_single () && count ($posts) > 0)
to just before remove_filter ('the_posts', array (&$this, 'the_posts'));
, it ceases the infinite redirect, and seems to still function fine! The only side-effect is that if you go to /jim/
it will redirect you back to /people/jim/
(rather than just giving you a 404). For me, that was Good Enough, since it fixed this problem.
So, again, within advanced-permalinks.php
, search for function the_posts
and then comment it so it looks like the code below.
Why does this break it? Because the custom post type is_single and doesn't match any rules, apparently, which makes APL send it back for re-direction... endlessly. Blah. Perhaps there is a more clever way to do this, to check if it is a custom post type, or whatever, but just disabling it seemed to do fine by me.
/**
* Hook that is called when a post is ready to be displayed. We check if the permalink that generated this post is the
* correct one. This prevents people accessing posts on other permalink structures. A 301 is issued back to the original post
*
* @return void
**/
function the_posts ($posts)
{
/* DISABLED CODE BELOW:
// Only validate the permalink on single-page posts
if (is_single () && count ($posts) > 0)
{
global $wp, $wp_rewrite;
$id = $posts[0]->ID; // Single page => only one post
// Is this a migrated rule?
$migrate = get_option ('advanced_permalinks_migration_rule');
if ($migrate)
{
if (isset ($migrate[$wp->matched_rule]) && substr (get_permalink ($id), strlen (get_bloginfo ('home'))) != $_SERVER['REQUEST_URI'])
{
wp_redirect (get_permalink ($id));
die ();
}
}
else
{
// Get the permalink for the post
$permalink = $this->get_full_permalink ($id);
// Generate rewrite rules for this permalink
$rules = $wp_rewrite->generate_rewrite_rules ($permalink);
// THIS IS ESPECIALLY PROBLEMATIC PART FOR CUSTOM POSTTYPES
// If the post's permalink structure is not in the rewrite rules then we redirect to the correct URL
if ($wp->matched_rule && !isset ($rules[$wp->matched_rule]))
{
wp_redirect ( get_permalink ($id));
die ();
}
}
}
DISABLED CODE ABOVE */
remove_filter ('the_posts', array (&$this, 'the_posts'));
return $posts;
}
精彩评论