There is an action 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.
For more information see parse_query.
Related Posts:
- Why is the loop not empty on some 404s?
- Pagination shows 404 after a certain number of pages
- Pagination causes error 404 when used with front-page.php
- Custom page template query_vars
- WP-PageNavi gives me a 404 when using WP Query
- Intentionally exceed max_num_pages on main query without getting 404?
- Difficulty with rewrite rules
- Custom pagination with WP_Query generating 404 error
- if/else on custom query gives 200 OK when condition not met?
- Have functions work outside the loop with 404 override
- Rewrite rule to prettify two $_GET variables while in a new endpoint from a page
- remove query arg from url after set query
- WordPress add_rewrite_rule() cannot visit lower url levels
- How to return a 404 when custom query matches nothing?
- Pre-Populate $wp_query settings with custom rewrite rules and custom template_redirect
- paged variable not working for category/custom post archive
- Issue with front page navigation after upgrading to 3.4
- Rewrite gets completely ignored
- Show a 404 error page if Public query variable’s value doesn’t exist?
- How to use if($wp_query->query_vars[‘name’] == ‘pagethatdontexist’) without getting a 404? How to suppress 404
- query_vars empty when using custom url with custom rewrite rule
- Add URL Rewrite Rule To WordPress
- is_404() – where does this get set?
- Custom Taxonomy Breaks Pages
- Reciving Error 404 blog/page/2/
- WordPress rewrite with custom parameter doesn’t work
- Generate custom output on URL with directory
- Show a message if there are no active posts in category
- Own query vars are not displayed
- WordPress Prevent 404 on missing custom post
- Pagination with Custom Post Type not working on index.php: I get a 404
- taxonomy-{term}.php terms pagination returning 404 after a certain page
- Get post count of current loop when using multiple queries on one page
- How do I query for posts by partial meta key?
- Display products from specific category in shop page
- How to uniquely identify queries?
- meta_query where value is equal to given value
- What’s the best approach for showing posts by A->Z order?
- page was loaded over HTTPS, but requested an insecure image
- WP_Query: How do I sort on meta value and use LEFT JOIN?
- WooCommerce: filter by parent product’s taxonomy and product variation’s meta data
- How to search by title or tags exclusively?
- Paginated pages are showing correct content but pagination links are not
- Query Posts but only return 1 from a custom taxonomy?
- Loop inside the loop
- WordPress search exact match
- What is the difference between RELATION “AND” and “OR” in TAX_QUERY?
- Query Set Order By Author
- ‘&’ causes an error in my shortcode when I list the content of the page
- wp_query, give first post different formatting
- Best practice custom function, where to echo the variables?
- meta_query works locally but not on live server
- getting posts and number by specific meta value in multiple meta
- How can I pick a single post from the latest 3?
- Retrieving list of a custom post type in a widget without using WP_Query?
- How to orderby meta_value_num with dollar ($) sign
- Order WP_Query by multiple fields, subtracting them from one another
- How can I convert results of WP_Query (of Custom Post Type, with Custom Fields) to JavaScript?
- How to order posts in wp_query by the user role (2 roles in array)
- getting post thumbnail within loop causes an error
- Reset postdata to custom query in nested queries
- Custom Query num_rows returns wrong amount
- Most commented post showing 2 records
- Loop Split two Columns
- Add posts to WP Query object
- use of 10 new WP_Query : loading too slow (with url) [closed]
- How to not display tags with less than X posts
- Order or Orderby in tax_query (How to define order of terms in WP_Query)
- Wp_query with 2 meta keys and array of meta values
- Create a loop on my pages with new “WP_Query”
- Which meta_query and post_content blend is better in WP_Query, performance wise?
- get_query_var always returns the default value
- WP ForLoop to compare meta information of posts to determine what post to display
- Problem with get_page_by_path() using with WP_Query
- Multiple wp_query on archive page
- change posts_orderby of the callback wp_query?
- WP_query is not returning the expected result
- the_post_thumbnail do 2 queries. How to optimize
- wp query meta value is not null
- Trying to use WP_Query to display a custom post type
- WP Query / Meta Query [duplicate]
- WP_Query and pagination AGAIN?
- Can’t get term id for category archive
- Nearby locations using Advanced custom fields, maps?
- View list of custom posts within a date range relative to today’s date, based on a custom date field
- How to display the category dropdown auto search list when key press?
- order by post date and modified date posts both in wpquery
- Comparing Meta Field date in WPQuery using Meta_Query?
- Fix wp_term_relationships slow query in get_posts
- Global page ID variable empty error
- Query postmeta based on meta_value, return array of post_id
- datetime picker, timestamps and meta queries
- Meta key in wp_query bug?
- How can I create another instance of my custom shortcode
- How To Call WP_Query From A Subdomain?
- WP_Query for attachments without duplicating post_parent and displaying tagged image
- Post OrderBy slow performance
- Showing posts from 4 categories along with all latest posts
- Searching in specific custom post type
- WordPress pagination returns the same posts