Search code examples
phpwordpressurl-rewritingtaxonomycustom-taxonomy

How to get the Following URL Structure in WordPress?


I have a

  • Custom Post Type - Awards (slug = awards)
  • Custom Taxonomy - Award Giver (awards-giver) (e.g. Oscars, Golden Globe, etc)
  • Custon Taxonomy - Awards Types (award-type) (e.g. Best Actor, Best Film, etc)
  • Custom Taxonomy - Award Year (award-year)

Now I want to dynamically have pages on my site with the following URL Structure

example.com/awards/oscars/year/2011

example.com/awards/oscars/best-actor/ (which will list all the winners of best actor award till now of all years)

I need to get these url structures working in WordPress. Any Advice?

Thanks in Advance :)


Solution

  • Example for: example.com/awards/oscars/year/2011

    This might help you. Put below code into your functions.php file:

    function custom_rewrite_basic() {
        add_rewrite_rule('^awards/([A-Za-z0-9._-]+)/year/([0-9]+)/?', 'your_actual_working_file_url.php?award_type_slug=$matches[0]&year=$matches[3]', 'top');
    }
    add_action('init', 'custom_rewrite_basic');
    
    RewriteRule ^awards/([A-Za-z0-9._-]+)/year/([0-9]+)/?$  your_actual_working_file_url.php?award_type_slug=$1&year=$2
    

    Make sure you put your actual working file url/path in above code as per your custom template file name or default template file name.