You can use an earlier action, request
, to do whatever you need. It allows you to modify query variables, or even cancel the entire request without issuing a 404 error:
function wpse112929_cancel_request_parsing( $query_vars ) {
if ( in_array( 'pagethatdontexist', $query_vars ) ) {
return array();
}
return $query_vars;
}
add_action( 'request', 'wpse112929_cancel_request_parsing' );
I used in_array()
here instead of a direct comparison, because
the actual match
depends on your permalink structure.
It would be name
for /%postname%/
, category_name
for /%category%/%postname%/
, pagename
for /%year%/%monthnum%/%postname%/
, etc.
You can also check $_SERVER['REQUEST_URI']
instead.