I'm having trouble trying to learn how to write this url into a regex template to add in as a rewrite. I've tried various regex sandboxes to figure it out on my own but they won't allow a '/' for instance when I copy an expression from here for testing: enter image description here

I've got a custom post type (publications) with 2 taxonomies (magazine, issue) which i'm trying to create a good looking url for.

So after many hours i've come here to find out how I can convert this


to a templated regex expression (publication, magazine and issue are constant) that can output


and hopefully with room to extend if an article is followed through from that page.

Thanks in advance.

  • 1
    the built in page post type rules already capture that particular pattern, you will break any page/sub-page requests. this is why custom post types are prefixed with a unique slug.
    – Milo
    Aug 19 '16 at 15:22
  • But the post type and those 2 taxonomies can be called explicitly.
    – 4t0m1c
    Aug 19 '16 at 16:25
  • Not sure what that means. Rewrite rules are for translating incoming requests into the correct query vars to generate a successful main query. There's no way for WordPress to differentiate test-mag or 2016-aug from some-random-page and some-random-subpage just from the URL alone.
    – Milo
    Aug 20 '16 at 3:07

From the wordpress documentations - https://codex.wordpress.org/Using_Permalinks

Using %category% with multiple categories on a post

When you assign multiple categories to a post, only one can show up in the permalink. The categories are ordered alphabetically. In each group of sub-categories the order will also be alphabetical. (see Manage Categories). The post will still be accessible through all the categories as normal.

You can however reach what you want by creating a page with the slug listpublications and adding the folllowing code:

add_action('init', 'rewrite');
add_filter('query_vars', 'query_vars');

function rewrite(){
    add_rewrite_rule('listpublications/([^/]+)/([^/]+)/?$', 'index.php?pagename=listpublications&magazine=$matches[1]&issue=$matches[2]','top');

function query_vars($query_vars) {
    $query_vars[] = 'magazine';
    $query_vars[] = 'issue';
    return $query_vars;

Now go to settings -> permalinks and click save. This will add the new rewrite rules, so this is very important.

Now create a template file in your theme folder named page-listpublications.php and add the following code between the footer and header.

 global $wp_query;

    $query_args = array(
    // show all posts matching this query
        'posts_per_page'    =>   -1,
    // show the 'publications' custom post type
        'post_type'         =>   'publications',
        // query for you custom taxonomy stuff
        'taq_query' => array(
                'taxonomy'  =>   'magazine',
                'field'     =>   'slug',
                'terms'     =>   $wp_query->query_vars['magazine']
                'taxonomy'  =>   'issue',
                'field'     =>   'slug',
                'terms'     =>   $wp_query->query_vars['issue']


   //fetch results from DB
    $query = new WP_Query( $query_args );

    if ($query->have_posts()):  while ($query->have_posts()): $query->the_post(); 
     // do something sweet with the results

Visiting www.yourdomain.com/listpublications/test-mag/2016-aug should give you all publications in test magazine and in issue 2016-aug.

Hope this helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.