Make WordPress Core

Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#19241 closed defect (bug) (fixed)

custom post type with extra rewrite rules doesnt work with permalin structure with %category%

Reported by: moz667's profile moz667 Owned by: duck_'s profile duck_
Milestone: 3.4 Priority: normal
Severity: normal Version: 3.0
Component: Canonical Keywords:
Focuses: Cc:

Description

I have registered a custom post type with an extra rewrite rule. When I set the permalink structure with %category% and access to the post type custom permalink structure (including the extra rule) allways get redirect to the post type itselfs.

To get the error follow this instructions:

  1. Install and activate this plugin (it register the custom post type and the extra rewrite rules
<?php 
/*
Plugin Name: Post Type Businesses Lists Bug
Plugin URI: http://example.com/
Description: 
Version: 0.0.1
Author: moz667
Author URI: http://example.com/
*/

add_action("init", "register_post_type_bss_list");
function register_post_type_bss_list() {
	$args = array(
		"labels" => array("name" => __("Businesses Lists")),
		"public" => true,
		"show_ui" => true, 
		"show_in_menu" => true, 
		"query_var" => true,
		"rewrite" => array("slug" => "bss-lists", "with_front" => false ),
		"capability_type" => "bss_list",
		"has_archive" => true, 
		"supports" => array("title", "editor")
	);
	
	register_post_type("bss_list", $args);
	
	$rules = get_option( "rewrite_rules" );
	
   	if ( !isset($rules["bss-lists/?$"])) {
   		flush_rewrite_rules();
   	}
}

add_action("wp_loaded","bl_flush_rules" );
function bl_flush_rules() {
	$rules = get_option( "rewrite_rules" );
	
	if (!isset( $rules["bss-lists/(.+?)/detail/(.+?)$"]) ) {
		flush_rewrite_rules();
	}
}

add_action("generate_rewrite_rules", "bl_rewrite_rules");
function bl_rewrite_rules($wp_rewrite) {
	$new_rules = array(
		"bss-lists/(.+?)/detail/(.+?)$" => 'index.php?bss_list=$matches[1]&bssname=$matches[2]'
	);
	
	$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

add_filter("query_vars", "bl_query_vars");
function bl_query_vars($public_query_vars) {
	$public_query_vars[] = "bssname";
	
	return $public_query_vars;
}

register_deactivation_hook( __FILE__, "bl_deactivate" );
function bl_deactivate() {
	flush_rewrite_rules();
}

add_filter("the_content", "bl_the_content");
function bl_the_content($content = "") {
	global $wp;
	
	if (isset($wp->query_vars["bssname"])) {
		$content = "<p>SOME DETAIL CONTENT FOR " . $wp->query_vars["bssname"] . "</p>";
	} else {
		$post = get_post(get_the_ID());
	}
	
	if ($post && $post->post_type == "bss_list") {
		$content = "<h4><a href=\"" . get_permalink() . "detail/sample-detail/\">Some detail link</a></h4>" . $content;
	}
	
	return $content;
}
  1. Set up the permalink structure at as : /%category%/%postname%/%post_id%/
  1. Add a new post type bss_list and publish it.
  1. Access to the view of the new post type bss_list.
  1. Click on "Some detail link", it is a link to a http://yourtestsite.com/bss-lists/your-new-bss-list/detail/sample-detail/ . You'll see how it is redirect to the custom post type again : http://yourtestsite.com/bss-lists/your-new-bss-list/

If you change de permalink structure to /%year%/%postname%/%post_id%/ it works properly, so I think that the %category% tag is the reason of the fail

I have been testing everything for a week, if you could help me I will be really grateful

Attachments (1)

19241.check-category_name.diff (1.1 KB) - added by duck_ 13 years ago.

Download all attachments as: .zip

Change History (6)

#1 @duck_
13 years ago

  • Component changed from General to Canonical
  • Version set to 3.0

This is caused by the canonical redirect that checks for is_single() and %category% in the permalink structure. It then does a redirect even if a category name wasn't included in the query (e.g. for a custom post type request, or your extra rule). See #11907, [13781] and [13798].

One fix could be to check for get_query_var( 'category_name' ) before performing a redirect.

#2 @dd32
13 years ago

19241.check-category_name.diff

Looks sane to me

#3 @moz667
13 years ago

Thanks a lot.

I'll try later.

#4 @duck_
13 years ago

  • Owner set to duck_
  • Resolution set to fixed
  • Status changed from new to closed

In [19970]:

Ensure that a category is part of the query before redirecting permalink structures containing %category%. Fixes #19241.

This fixes incorrect canonical redirects for singular queries using custom rewrite rules.

#5 @duck_
13 years ago

  • Milestone changed from Awaiting Review to 3.4
Note: See TracTickets for help on using tickets.