WordPress: Fix for WordPress 404 error on custom routes

When working with custom routes in WordPress, you may encounter the issue where WordPress mistakenly recognises your route as a 404 page. This can be frustrating, especially when you have correctly configured your route. Fortunately, there is a simple solution to this problem.

To tell WordPress not to treat your custom route as a 404 page, you can use the following code:

$wp_query->is_404 = false;

This code should be placed in your theme or plugin's functions.php file. By setting $wp_query->is_404 to false, you are telling WordPress that the current page is not a 404 page and it will therefore treat it correctly as a custom route.

Example code snippet


	function filter_template( $template ) {
		global $wp_query;
		$cars = ['audi', 'bmw', 'mercedes'];
		foreach ( $cars as $car ) {
			if ( isset( $wp_query->query['pagename']) and  $wp_query->query['name'] == $model ) {
				$wp_query->is_404 = false;
				$template = __DIR__ . '/single.php';
			}
		}
		return $template;
	}
    In the provided code snippet, I have a function called "filter_template". The function takes a template as input and returns a modified template. Here is an explanation of what happens in the code snippet:

  • I define a global variable "$wp_query" which provides access to information about the current query on the page.
  • I define an array of car brands (in this case ['audi', 'bmw', 'mercedes']) that represent the car brands you want to work with.
  • I start a foreach that iterates through each car brand in the array "$cars".
  • Inside the loop, I check if the variable "$wp_query->query['pagename']" is defined and if the name of the query ("name") matches the current car brand ("$car").
  • If the condition is true, I set "$wp_query->is_404" to false, which tells WordPress that the current page is not a 404 page.
  • I change the value of the variable "$template" to the path "DIR . '/single.php'", which indicates the new template to use.
  • Finally, I return the modified template.

Manipulate the post type with $wp_query->is_singular = 'car'

If you are using custom routes in WordPress and want to manipulate which post type is retrieved in your query, you can make use of $wp_query->is_singular = 'car' to set the desired post type to 'car'. This can be useful, especially if you are already using the wp core function is_singular('car') elsewhere in your code.

By manipulating $wp_query->is_singular in this way, you can control which record type is handled in your custom route and customise the logic or output based on that record type.


$wp_query->is_singular = 'bil';

Here you need to replace 'car' with the name of the desired record type you want to retrieve in your route. This ensures that $wp_query is aware that the query is targeting a single record of type 'car'.

When directly manipulating $wp_query->is_singular, there is a potential risk of error as it can affect the functionality of wp-includes/general-template.php. Although the exact cause of this error has not been investigated, I opted instead to take an alternative approach to data manipulation by utilising the $wp_query->is_post_type_archive = 'car' function.

Either way, the purpose is to apply a condition in my code that ensures relevant changes are only implemented when working with pages that have this condition. This allows me to make specific adjustments or customisations that only affect that area, while preserving the general functionality of other parts of the code.


$wp_query->is_post_type_archive = 'bil;