1

Am using a third-party plugin for listing schools.

Custom Post Type = school

Custom Post Taxonomy 1 = school_type (eg. values = Primary, Intermediate)

Custom Post Taxonomy 2 = school_location (eg. values = Auckland, Otago)

In my current setup, each school (individual posts) can be accessed like this:

http://example.com/school/snells-beach-primary
http://example.com/school/murrays-bay-intermediate
...etc

And URL of school types taxonomy would look like these:

http://example.com/school_type/primary
http://example.com/school_type/intermediate
...etc

URL of school locations taxonomy would look like these:

http://example.com/school_location/auckland
http://example.com/school_location/otago
...etc

What am trying to achieve is, after the http://example.com/school/, whatever there is in the slug should check for the individual post slug and route, if not found, then it should check either school_type or school_location taxonomies and pull the data.

So in the above examples, the new URLs would look like these:

-- individual posts
http://example.com/snells-beach-primary
http://example.com/murrays-bay-intermediate
...etc

-- school type taxonomy (as archives)
http://example.com/primary
http://example.com/intermediate
...etc

-- school location taxonomy (as archives)
http://example.com/auckland
http://example.com/otago
...etc

I installed the Rewrite Rules Inspector plugin to check the current rewriting to view the current rewrite rules. Based on that I added the following code as a new plugin:

add_action( 'init',  function() {
    add_rewrite_rule( 'school/([a-z0-9-]+)[/]?$', 'index.php?school_location=$matches[1]', 'top' );
    add_rewrite_rule( 'school/([a-z0-9-]+)[/]?$', 'index.php?school_type=$matches[1]', 'top' );
} );

The above will work for accessing these URLs now (for school_type taxonomy):

-- school location taxonomy (as archives)
http://example.com/auckland
http://example.com/otago

But both the individual school post URL and the school_location URLs won't work after this. Then I tried changing the third parameter in add_rewrite_rule in my code, to bottom for both lines. And then the individual school post URLs are working, but both the school_type and school_location URLs are not!

So how can I add the rewrite rules( for the URL: http://example.com/XXXXX) in the following order of precedence:

- XXXXX = individual school post
- XXXXX = school_post or school_location

EDIT

After some more research, I found that if there are multiple rewrite rules with same regex, only one will make it to the rewrite_rules_array (based on the top or bottom priority).

So I tried slightly tweaking the regex, so that all my rules will be included :

add_action( 'init',  function() {
    add_rewrite_rule( 'school/(.+?)[/]?$', 'index.php?school=$matches[1]', 'top' );
    add_rewrite_rule( 'school/([a-z0-9-]+)[/]?$', 'index.php?school_location=$matches[1]', 'top' );
    add_rewrite_rule( 'school/([0-9a-z-]+)[/]?$', 'index.php?school_type=$matches[1]', 'top' );
} );

Again flushed and checked using the Rewrite Rules Inspector. Found that my 3 rules were present in rewrite_rules_array. But unfortunately, only the first rule is matched and then it will stop looking at the other rules. So if there's a school post, then it will be displayed, otherwise a 404 page. It's not checking the next rule!

How can I resolve this issue?

  • You are not going to be able to do this sort of thing with multiple "rewrite rules" (and these rules do conflict). A rewrite rule is processed and the request ends (essentially). There is no concept of a "return state on failure" so that the next rule can be processed. – MrWhite Aug 20 at 0:33

Your Answer

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

Browse other questions tagged or ask your own question.