Create Own Rewrite Rules in Wordpress

Create Your Own Rewrite Rules


In this example we are going to create a rule that will create a url of /actor/%actor-name%, once we get this URL we will pull out the %actor-name% from the URL and search for all posts which have this actor name as a tag attached to the posts.
add_action('init', 'add_actor_url');
function add_actor_url()
{
    add_rewrite_rule(
        '^actor/([^/]*)$',
        'index.php?tag=$matches[1]',
        'top'
    );
}
Now if I was to add a tag onto a post I can search for it by using the /actor/%tag-name% URL, this will display the tag page with all the posts that have this tag attached to them.
You can change these to anything you want, if you want to search on year and get all the posts in this year you can do so easily, you can even use a URL of /iwanttosearchonyearposts/2014.

Know all Url rewrite tricks in Wordpress
add_rewrite_rule(
        '^iwanttosearchonyearposts/([^/]*)$',
        'index.php?year=$matches[1]',
        'top'
    );

Comments

Popular Posts

Answers Of Amazon Pay Quiz