Flush wp_rewrite rules

As stated on WordPress Codex: Flushing the rewrite rules is an expensive operation. So it’s best to refresh your wp_rewrite rules by not using the init hook.  Austin Passy chooses to Flush your rewrite rules on plugin/theme activation on checking $_GET parameters.  I’ve tried it two different ways.  The first option seemed to work for a while, but my added rule disappeared somehow and began sending requests to a 404 page so I started using the other option.

Option 1:

$custom_permalink_rules_exist = get_option( $this->permalink_rule_option_name );

if( $custom_permalink_rules_exist != "1") {
	add_action( 'init', 'flush_rewrite_rules');
	add_action( 'generate_rewrite_rules', array( &$this, 'add_rewrite_rules' ) );

	update_option( $this->permalink_rule_option_name, '1');

Option 2:

	 * Flushing the wp_rewrite rules is an expensive proceedure so check
	 * if the rules exist
	 * @global object $wp_rewrite
	function verify_rewrite_rule_exists() {
		global $wp_rewrite;

		if( empty($this->rewrite_rule_key) ||
			!array_key_exists($this->rewrite_rule_key, $wp_rewrite->rules ) ) {
			add_action( 'generate_rewrite_rules', array( &$this, 'add_rewrite_rules' ) );

	function add_rewrite_rules( $wp_rewrite ) {
		global $wp_rewrite;

		$this->rewrite_rule_key = "^{$this->post_type_slug}/([^/]*)/([^/]*)/([^/]*)$";

		// match a custom post_type with a parent and child taxonomy
		$new_rules = array( "^{$this->post_type_slug}/([^/]*)/([^/]*)/([^/]*)$" => "index.php?{$this->post_type_slug}=" . $wp_rewrite->preg_index(3) );

		// match a custom post_type with only one taxonomy; however, will break the archive page for two taxonomies
		//$new_rules = array( "^{$this->post_type_slug}/([^/]*)/([^/]*)$" => "index.php?{$this->post_type_slug}=" . $wp_rewrite->preg_index(2) );

		// Add the new rewrite rule into the top of the global rules array
		$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;


		return $wp_rewrite->rules;

Custom Post Type redirects to 404 page

This function from dev4press really helped me debug my custom post type redirect to a 404 page.


Place this code in your functions.php file:

function dev4press_debug_page_request() {
		global $wp, $template;

		echo '<pre>!-- Request: ';
		echo empty($wp->request) ? "None" : esc_html($wp->request);
		echo ' -->'.PHP_EOL;
		echo '!-- Matched Rewrite Rule: ';
		echo empty($wp->matched_rule) ? None : esc_html($wp->matched_rule);
		echo ' -->'.PHP_EOL;
		echo '!-- Matched Rewrite Query: ';
		echo empty($wp->matched_query) ? "None" : esc_html($wp->matched_query);
		echo ' -->'.PHP_EOL;
		echo '!-- Loaded Template: ';
		echo basename($template);
		echo ' --></pre>'.PHP_EOL;

Place this in your template file:


echo '<h1>Rewrite Rules</h1>';

global $wp_rewrite;

echo '<pre>';
echo '</pre>';

Breadcrumbs for Taxonomy for a Custom Post type

This is what I came up with to display the category, or rather a custom taxonomy, of a single posttype page of use in a breadcrumb. The taxonomy I used is ‘smg-logo-brand’ and the terms slug where ‘kcrg’, ‘the-gazette’ and ‘sourcemedia.’ I compared the terms associated with the post to that in the URL to determine which term belonged in the breadcrumb.

if (!is_tax() && get_post_type() == 'smg-logo' && has_term( array( 'kcrg', 'the-gazette', 'sourcemedia', 'other' ), 'smg-logo-brand' ) ) {
			$terms = get_the_terms( get_the_ID(), 'smg-logo-brand' );
			foreach ( $terms as $term ) {
				if ( strpos( $_SERVER["REQUEST_URI"], $term->slug ) !== false) {
					$breads['terms'] = '<a href="/logos/'. $term->slug . '/">' . $term->name . '</a>';