I was able to display a 404 error by using the following code in my header.
<?php
global $wp_query;
$wp_query->set_404();
status_header( 404 );
get_template_part( 404 ); exit();
?>
To break it down:
$wp_query->set_404(): tells the wp_query this is a 404, this changes the title
status_header(): sends a HTTP 404 header
get_template_part(): displays the 404 template
Related Posts:
- How to force a 404 on WordPress
- How to prevent automatic redirection of 404 errors and “incorrect” URLs?
- How do I skip wordpress’s 404 handling and redirect all 404 errors for static files to 404.html?
- How do I programmatically generate a 404?
- How do I skip wordpress’s 404 handling?
- 404 redirection and Google’s index [closed]
- Redirect feed to 404 page
- Check if post is in draft or pending review?
- Why specific file directory fails to redirect but its parent folder succeeds?
- WordPress blocking/preventing viewing of .cgi file in subdirectory [closed]
- Deny access to a path (give 403 or 404 response)
- Redirect a page id url but not the page slug
- Defining a custom 404 for non-WordPress parts of site
- WordPress Auto-Redirecting on 404
- Built-In Postname Redirection vs Hard Redirects in relation to SEO and performance
- How to redirect a wrong search on my wordpress website to the error 404 page?
- WP page 404 error only on mobile
- Force 404 redirect to home?
- How can I redirect users on the new 404 page without plugin?
- How to redirect an old page url to the homepage
- Dynamic 404 page content while still keeping 404 status code?
- Redirect to home if page doesn’t exists [duplicate]
- Redirect to parent page if child does not exist
- How to redirect non existent pages/posts in wordpress using htaccess
- How to Redirect WordPress domain.com/?anyword to 404 Page?
- How do I redirect the browser to 404 page, if no posts are found in home page
- How do i make WordPress return 404 instead of redirecting on misspelled page?
- How to redirect if 404 error in wordpress then redirect 404.php url
- Redirect to /404/
- Page editing in backend redirecting to 404 page not found with url /post.php
- How do I skip wordpress’s 404 handling?
- Subpages return 404 server error
- WordPress subfolders htaccess bypassing the root htaccess RewriteRule
- Why my htacess rewrite return 404?
- How to Redirect Url to homepage?
- Stopping media file redirects
- Forcing header cache control in 404 pages
- redirect to 404 page
- Redirect certain 404 pages to parent page
- Redirect of specific words in URL ahead of website migration
- Disable front end to use as CMS only?
- wp_redirect() function is not working
- My wordpress multisite homepage redirects to signup page
- Login redirect to previous page
- Prevent access to single post types
- WordPress install enters an infinite loop
- handling login/logout redirects
- Using `auth_redirect` : keeps asking me to login even when I’m logged in
- Redirecting to home-page when saving any edited code
- Can WordPress redirect to a “similar page” in case of 404 error
- Hacked website redirect, only on desktop, help with restoring it [closed]
- Can I check for maintenance mode before redirecting to subdomain?
- Custom Registration page – prevent redirect to wp-login.php?action=register
- How to target specific user role?
- How to auto login after registration? [duplicate]
- homepage redirects to login page when user session expires
- Redirect blog archive into certain format
- Auto-detect the redirect url from the original url
- Custom Permalink
- How can I replace my primary url globally with a parked one?
- My website on checking showing 403 error on HTTP Status [closed]
- How do I determine what is causing a redirect for a given page?
- Trouble running local WordPress in Google App Engine
- Empty folder being created by WordPress, breaks site
- !is_admin() condition turns true in admin pages
- I used single quote to design HTML of my WordPress blog. Now site redirects to index.php everytime than page.php or single.php
- WordPress index.php seems to perform unwanted redirect 301
- Issue redirecting ‘blog’ to ‘news’ in WordPress using .htaccess
- wp_redirect and current_user_can issues
- Redirect /home to home.php
- WordPress localhost site always redirect to live site [closed]
- Redirect old query string URLs to new add_rewrite_rule URL
- How can I set up a splash page that is redirected to only one time for the visitor? [closed]
- Temporary Infinite Redirect Loop
- Add new post redirection [closed]
- Blog keeps redirecting to the contact page
- WordPress Pagination – 301 Redirect Issue
- Why do my new WordPress pages redirect to home / staging site?
- WordPress multi domain: page redirect to main domain whenever WordPress do the URL guessing issue
- Puzzled at HTTP/2 301 response header
- How to regenerate RSS feed URL like FeedBurner?
- How to avoid redirect 302 in wordpress to the author pages?
- ErrorDocument 410 gives me back a 302 header
- WordPress Staging site is redirecting to live and lost the child theme
- Mailjet Email Subscription + Redirect w/ Custom Template Fails
- htaccess Redirect 301 doesn’t work. How to debug?
- Display a custom 404 page without a redirect
- change home_url and site_url but don’t redirect to home_url and site_url on load
- My WordPress site redirects to port 8082
- wp-login.php redirect problem
- International characters in slugs to redirect to
- How to Block WordPress from subdomain?
- WordPress redirect to splash page once a day for the first week
- after logout, get_current_user_id still holds value
- Solution for WordPress URL structure changes and 301 redirects?
- Subdomain add comment redirect to 404 not found page iis server
- How to change Ugly link?
- Redirected You too Many Times and Homepage Not Loading
- Redirect all pages to a specific page of a new domain but exclude specific pages
- Make category page the wordpress homepage (not blog)