[wp-hackers] Need Custom Post Type Rewrite Help, Please

Mike Schinkel mikeschinkel at newclarity.net
Sun Apr 18 18:01:43 UTC 2010


Okay, so the final fix needed was this:

add_filter('post_type_link','wbt_post_type_link',10,3);
function wbt_post_type_link($link, $id, $leavename) {
	if (strpos($link,'financial-terms/')!==false)
		$link = str_replace('financial-terms/','',$link);
	return $link;
}

Not the cleanest, doing string replace, and I'd rather have a hook called once for register_post_type() than once per hyperlink, but it works for now.

Thanks scribu and Christian for the help.

-Mike

On Apr 18, 2010, at 1:37 PM, Mike Schinkel wrote:

> On Apr 18, 2010, at 6:49 AM, scribu wrote:
>> On Sun, Apr 18, 2010 at 1:37 PM, Mike Schinkel
>> <mikeschinkel at newclarity.net>wrote:
>> 
>>>      $pages = get_pages();
>>>      foreach ( $pages as $page )
>>> 
>>> $wp_rewrite->add_rule("$page->post_name/?","index.php?post_type=page&p={$page->ID}",'top');
>>> 
>>> Only problem is, in either case, now it calls single_template instead of
>>> page_template.  Go figure.
>>> 
>> 
>> 
>> You should use *?page_id=* instead of *post_type=page&p=*
> 
> 
> Awesome, that did it. Thanks!  
> 
> But unfortunately it's still not 100% complete.  This is the code that works thus far:
> 
> 		global $wp,$wp_rewrite;
> 		$wp->add_query_var('financial-term');
> 		$wp_rewrite->add_rewrite_tag('%financial-term%', '([^/]+)','financial-term=');
> 		$wp_rewrite->add_permastruct('financial-term', '%financial-term%');
> 		$pages = get_pages();
> 		foreach ( $pages as $page ) {
> 			$wp_rewrite->add_rule("$page->post_name/?","index.php?page_id={$page->ID}",'top');
> 		}
> 		$wp_rewrite->flush_rules(false);
> 
> Unfortunately I'm finding that get_permalink() returns 'example.com/financial-term/term-name/' (which generates a 404) instead of 'example.com/term-name/' which serves as I want it to.  I can of course hook 'post_link' and manipulate the link but it's like I shouldn't have to do it that way and thus am doing something wrong.  
> 
> OTOH, looking just now at the definition of register_post_type() it seems to give no other option than something like '%post_type%/post-name'.  Any ideas, or is this just where a new filter is needed?
> 
> -Mike
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers



More information about the wp-hackers mailing list