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
- Why am I getting a 301 on a existing post?
- Non-WordPress page in subdirectory under WordPress page
- Custom login form redirect to external site
- Clarification on auth_redirect()
- Exclude a path from WordPress using .htaccess redirects (Apache)
- Restrict Access to wp-login.php
- Stop unwanted WP redirection to new url
- Problem to redirect custom page using htaccess
- WordPress within a subdirectory of another WordPress environment doesn’t work
- Website redirecting to old domain name
- How to do 301 redirect to Sub page using htaccess file?
- Does WordPress automatically redirect url without parent slug?
- Redirect guest if he tries to access a specific page
- Localhost wordpress site redirects to public site
- Redirect homepage ( www.sitename.com ) to the latest post
- How to redirect URL with subfolder to the same URL but without subfolder?
- What is the right way to redirect all traffic to HTTPS?
- If User is NOT Logged in and Page ID is not 6 – Redirect to Login
- Front end user creation form! How do you keep the user on the same page in event of error?
- URL redirect problem
- How to redirect if a background request is still being executed
- WP site in infinite redirect loop
- WordPress keeps redirecting subpages without http to https homepage after switch
- Redirect non-existent subpages to second part of url
- 404 redirect to previous category
- How to remove the [L] Flag for WordPress index .php mod_rewrite
- ReDirect subfolder link to another sub-folder and force SSL
- How do I remove the RSS Feed Redirect?
- Redirect user to original url after login?
- User redirect to specific URL after logging in
- What is the most efficient (ie. less server intensive) way for 301 redirects?
- BuddyPress – Redirects on Login and Logout
- adding GET variables to a header() location in a page template
- WordPress CSS/Theme gone after SSL got activated
- Redirect 301 of old urls to wordpress urls
- Redirecting DNS without breaking everything
- redirect “about author” code to about page
- redirect doesn’t work while using example code
- redirect second domain to static page
- How can force redirected to login for two or more pages to view or access
- WordPress not linking correctly with .htaccess redirect to subdirectory
- WordPress installed on a virtual machine and behind a reverse proxy is redirecting to the VM’s IP address
- How to redirect?
- How to disable WordPress URL autocomplete / recognition?
- Redirect user on login using extra field value on login form
- login redirect problem – db charset issue?
- Custom login-form redirect
- Link to another website with the same url path
- Redirect to Page after Login (with variable)
- I have cloudflare flexible enabled, How to redirect to https for Nginx server
- can’t handle a redirect
- Login Form Redirection
- How can public access to WordPress query (e.g. author=) URLs be redirected?
- WordPress HTTPS – ‘… better to use HTTP for installation …’
- Hosting the WordPress website on someone else’s web URL
- Detect if request is coming from wordpress conditional statement
- WordPress on a subfolder but accessible from root
- Regex for pdf’s – Old .asp Site folder structure to new WordPress Media Folder(s)
- Redirect if a user has not written a post
- Create a custom redirect link that tracks the first visited page and after submitting form goes to first page using formidable forms plugin? [closed]
- WordPress not use site url when redirect 301