[wp-hackers] Custom URL Rewrite Help

Dobri dyordan1 at ramapo.edu
Tue Jul 2 12:41:52 UTC 2013


WP complies with the first rule matched. add_rewrite_rule adds your rule to the bottom of the list (I think). Thus, it's never reached. You have a better shot doing this with something like this: (straight out of my code so ignore variable names and stuff.)

function add_rewrite_rules($aRules) {
	global $press_releases_slug;
	$year = date('Y');
    $aNewRules = array(
        $press_releases_slug.'/([0-9]{4})/?$' => 'index.php?post_type='.$press_releases_slug.'&year=$matches[1]&posts_per_page=-1&nopaging=1&orderby=post_date&order=DESC',
        $press_releases_slug.'/search/(.+?)/?$' => 'index.php?s=$matches[1]&post_type='.$press_releases_slug.'&orderby=post_date&order=DESC',
        $press_releases_slug.'/?$' => 'index.php?post_type='.$press_releases_slug.'&year='.$year.'&posts_per_page=-1&nopaging=1&orderby=post_date&order=DESC'
    );
    $aRules = $aNewRules + $aRules;
    return $aRules;
}

add_filter('rewrite_rules_array', 'add_rewrite_rules');

As you can see, I create an array of my rules and prepend it to the existing ones. Presto, they become the first ones that match. Not my best piece of work but it is functional. I think add_rewrite_rule is more about adding something that doesn't conflict with the stuff wp builds by default. I guess another way would be when creating your post type to set rewrite to false. (http://codex.wordpress.org/Function_Reference/register_post_type) Whichever works for you. Enjoy!

~Dobri

On Tue, 2 Jul 2013, at 5:17 AM, Md Mahmudur Rahman wrote:

> Dear Hackers,
> 
> I have a custom post type (artist), which has a custom URL rewrite for a
> custom page template as given below:
> 
> *URL:* http://localhost/artist/artist-name/videos/3/
> 
> *Code for URL Rewrite:*
> 
> add_action( 'init', 'my_artist_rewrite_rule' );
> 
> function my_artist_rewrite_rule() {
> 
>            add_rewrite_rule( '([^/]+)/videos(/[0-9]+)?/?$',
> 'index.php?post_type=artist&name=$matches[1]&videos=yes&page=$matches[2]',
> 'top' );
> 
> 
>        }
> 
> And I am manually flushing the permalink settings each time I am making any
> change to the rewrite rule to reflect the changes. But it seems that the
> custom rewrite rule is not working. Here is the response of the $wp object:
> 
> [query_vars] => Array
>        (
>            [name] => artist-name
>            [page] => /3
>            [news] => yes
>            [post_type] => artist
>        )
> 
>    [query_string] => name=artist-name&page=%2F3&news=yes&post_type=artist
>    [request] => artist/artist-name/3
>    [matched_rule] => ([^/]+)(/[0-9]+)?/?$
>    [matched_query] => name=artist-name&page=%2F3
>    [did_permalink] => 1
> 
> So the requesting URL is always matching to the below rule ignoring the
> custom rewrite rule:
> 
>    [matched_rule] => ([^/]+)(/[0-9]+)?/?$
> 
> I have checked the custom rule with a rewrite rules inspector plugin and
> requested URL always matched with the custom URL rewrite rule but that is
> not the case in real.
> 
> Not sure what is happening behind? Hope someone can give me a clue.
> 
> *Many thanks,*
> 
> *Mahmudur Rahman*
> 
> <http://www.79mplus.com/>
> _______________________________________________
> 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