You should be using a filter outside of your template for this:
add_filter( 'template_include', 'wpa62226_template_include', 1, 1 );
function wpa62226_template_include( $template ){
if( is_page( 'some-page' ) ) :
global $wp_query;
$wp_query->set_404();
status_header( 404 );
$template = locate_template( '404.php' );
endif;
return $template;
}
Your code is executed before the template is loaded, so it becomes a non-issue.
Related Posts:
- Pages other than Home page not displayed even when URL is good
- 404 error when passing “cat” parameter to URL
- Change url links on members loops in buddypress
- Twitter shortlink 404 not found [closed]
- Multiple duplicate URL’s
- Moving Website-URL
- Adding more onto an esc_url [closed]
- How can I insert a dynamic URL in my email template?
- wp-login.php ampersand in url results in 404
- Avoid too long words in a post title, post content or comment but only if its not a url
- Difference between esc_url() and esc_url_raw()
- Remove All Query Arg
- Pulling a parameter out of the URL of a WP link without “?” or being sent to a different page
- Run WordPress frontend and backend in different domains
- How to set global variable in functions.php
- Adding a hook to ‘parse_request’ so that siteurl/pagename/xyz ignores “xyz”
- How to have numeric URLs in Posts and Pages
- Why WordPress automatically removes http from the url of my website’s sub domain?
- Pagination throws 404
- WordPress Move to SSL How to Update Media Assets to HTTPS?
- Moving wp-content outside of web root?
- Adding target and _blank as it’s value to the allowedtags
- permalink for category pages and posts
- Get url from file uploaded in Media Library
- Allow UTF-8 characters in the user slug part of URL
- WordPress converting ../url to http://../url
- home_url(); showing current page instead of site address
- Components used in Themes and Plugins: How to get file URL?
- How can I get the intended URL in a 404?
- Can URL remain the same if migrating from Drupal?
- Site URL always in front of other URLs
- Easiest way to simulate WordPress’s URL resolution to retrieve post ID, etc?
- How to add domain security certification, in WordPress website?
- Imported thumbnails have incorrect URL
- Is it necessary to check for `content-type` in headers?
- How to allow — in category name
- Request parameters in $_GET do not match URL called
- WordPress custom search url
- The requested URL was not found only on localhost
- Allow Chinese character on post URL slug
- How can I change a media attachment URL/slug, so that I can use that slug for a page?
- Problem with utf8 link
- remove query string from homepage url
- Remove Trailing Slash from Category Base and Tag Base
- How to handle this URL Structure. Keep getting “page not found”
- WP URL /icons returns 403 forbidden error
- Get root folder without domain
- wordpress.com website url structure [closed]
- WordPress get parameter ‘unapproved’ and ‘moderation-hash’
- URL issue after migrating to dev site
- Misentered the site address url. How to recover? [closed]
- Renamed my website (URL) – how do I rename all within-page links to the old URL?
- How to call for bloginfo inside function in functions.php?
- How to get CGI variables?
- What is the best way to figure out which template file that a “pretty permalink” is using?
- WordPress Subcategory Creates 2 urls for same page (serious issue)
- How to see an item in the admin from its URL? (not sure if it’s a page, post, or what)
- Can’t change the wordpress site url
- Cannot access WordPress website from external IP while internally it is working properly [closed]
- Pass form input via url variable
- Rewrite Most WordPress URLs
- Relative URLs within site
- How to make WordPress urls matching static html urls
- Using WordPress for a site’s static marketing pages and blog at different URLs
- What is the best way to revert to www version of domain name?
- Need to check if user is on a particular page
- Hook for feed creation?
- How to set path to the image instead of alt in media.php?
- Go back to my home page
- Changing url of author page
- How to get previous and Next Post Url in the_post_navigation
- Thousands of unwanted URLs/pages with “0” in slug created
- Can non-latin characters appear in slugs?
- Check current URL is 404 in pre_option_stylesheet filter hook
- Encode URL for WhatsApp “click to chat” without loosing accented characters
- append currency to URL
- Create 600 links to separate files on google drive
- Locked myself out of WordPress because I changed my URL and computer shut down
- Cant access my wordpress.org locally
- I changed the URL [closed]
- WP site homepage displays url link at beginning of each featured post
- Unable to login after changing WordPress site url
- Why is my WordPress URL shortened
- WordPress website giving 404
- How to create URL for Pages/Posts
- Private Messages between users
- Navigation and Permalink not getting correctly
- How to get the correct URL on my wordpress site?
- How to redirect WP post with a specific word in the URL
- Duplicate content with the same URL, google authorship
- Making a site URL
- Change URL to filter posts
- How can i maintain permalink structure and avoid a 404 error when loading external content?
- Modify WordPress to not replace + (plus) characters from URLs?
- Accessing a file saved outside webroot in a wordpress post
- Is There a Way to Schedule URL to Content Updates?
- I changed my url and cant access my site, Help?
- Accidentally changed the url
- How to make media URLs Unique
- What purpose does /embed/ URL have and how to avoid SEO problems?