Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How can you trigger a 404 when using custom query vars? I've got re-write rules written for a custom query var, but if you were to request a URL for the query var that should technically be a 404 it returns a normal WP page, but no content, because nothing technically exists for the URL.

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

There is a method specifically for this:

function my_parse_query( $wp_query ) {
    if ( $wp_query->get( 'my_custom_var' ) > 42 )
        $wp_query->set_404();
        status_header( 404 );
}
add_action( 'parse_query', 'my_parse_query' );

That should load the 404.php template in your theme, if you have it. Otherwise, it will fall back to index.php.

This will also trigger a HTTP 404 status code.

share|improve this answer
    
Thanks, do you have an example of how it could be implemented? –  hsatterwhite Mar 26 '11 at 19:57
    
Updated answer. –  scribu Mar 26 '11 at 22:53
add comment

Add a 404.php to your theme and look if it is called.

Sample 404.php

<!doctype>
<title>404</title>
<p>404 – Mwah!</p>

What is more important: the real HTTP headers, here the status code. You can see it with your browsers developer tools under network. Here is a screen shot for http://wordpress.stackexchange.com/404 in Opera Dragonfly:

screen shot

See full size image

If you still get a status code 200, look into the global variables $wp and $wp_query. They will tell you more details about the fetched resource.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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