• I need help adding redirect code for a 2nd post_type. I’ve successfully added a custom_post_type and it’s being redirected using the following code. My problem is that I can’t add another one for another post type that I also have. When I try it says “function add_new_rules already defined.” (or something similar) Is there a way to combine both redirects into one set of code?

    Thanks in advance for any help you can offer.

    add_new_rules();
    function add_new_rules(){
    
    	global $wp_rewrite;
    
    	$rewrite_rules = $wp_rewrite->generate_rewrite_rules('my_post_type/');
    	$rewrite_rules['my_post_type/?$'] = 'index.php?paged=1';
    
    	foreach($rewrite_rules as $regex => $redirect)
    	{
    		if(strpos($redirect, 'attachment=') === false)
    			{
    				$redirect .= '&post_type=my_post_type';
    			}
    		if(0 < preg_match_all('@\$([0-9])@', $redirect, $matches))
    			{
    				for($i = 0; $i < count($matches[0]); $i++)
    				{
    					$redirect = str_replace($matches[0][$i], '$matches['.$matches[1][$i].']', $redirect);
    				}
    			}
    		$wp_rewrite->add_rule($regex, $redirect, 'top');
    	}
    
    }
    add_action("template_redirect",'template_redirect');
    function template_redirect()
    {
    	global $wp;
    
    	$muley_custom_types = array("my_post_type");
    
    	if (in_array($wp->query_vars["post_type"], $muley_custom_types))
    	{
    		if ( is_robots() ) :
    			do_action('do_robots');
    			return;
    		elseif ( is_feed() ) :
    			do_feed();
    			return;
    		elseif ( is_trackback() ) :
    			include( ABSPATH . 'wp-trackback.php' );
    			return;
    		elseif($wp->query_vars["name"]):
    			include(TEMPLATEPATH . "/single-".$wp->query_vars["post_type"].".php");
    			die();
    		else:
    			include(TEMPLATEPATH . "/".$wp->query_vars["post_type"].".php");
    			die();
    		endif;
    
    	}
    }
  • The topic ‘Redirect more than one custom_post_type’ is closed to new replies.