1

I can't seem to find any documentation on this and Google is only showing results for people who have 404 pages caused by their incorrectly implemented taxonomies.

I want to show a custom 404 page for my custom taxonomy. I would hope that there would be a simple naming convention like with taxonomy-{taxonomyName}.php. Maybe something like 404-taxonomy-{taxonomyName}.php

If there is not anything like that, then what would be the best way to capture the attempt at a taxonomy page from the default main 404 template?

Is there anything in WordPress that could help with this, or would I have to parse the URL to determine if the user is on a taxonomy page?

Just in case it helps, here is an example of my use case.

A user uses Google Autocomplete to select their location. User then get's redirected to the taxonomy page of their location. EXAMPLE, user selects Toronto, Ontario, Canada and gets redirected to mysite.com/location/canada/ontario/toronto.

Currently, if those locations exist, the taxonomy template will kick in and show any posts that belong. If that location does not exist, then the standard 404 page show. I want to be able to show something like "sorry, but there are no listings in your area at this time".

Any thoughts or ideas would be very helpful!

2

You can filter any type of template to override the template hierarchy. In this case the filter is 404_template.

We'll check if the location query var is set, meaning the URL matched the pattern of a location request, but the result was a 404. In that case, we'll load the 404-taxonomy-location.php template.

function wpd_custom_tax_404( $templates ){
    if( '' !== get_query_var( 'location' ) ){
        $templates = locate_template( '404-taxonomy-location.php', false );
    }
    return $templates;
}
add_filter( '404_template', 'wpd_custom_tax_404' );

This code should go in your (child) theme's functions.php file.

  • This is EXACTLY the answer I was looking for. Thank you for the great answer. I think the template filter will come in very useful in other projects as well. – Harmonic Sep 12 '17 at 1:09

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.