2019-07-08 20:33:20 8 Comments
I have a couple of pages that are populated with data from non-Wordpress tables in my database. https://pageURL/?asi_id=12345 causes the DB-lookup to get the correct data. If someone uses the wrong query-string I want a 404 error generated. I am at a point long after the headers have been generated when I discover the issue. I have tried several code fragments from this forum and others. They cause the "404-template" to be displayed by the status code is still 200 and not 404. I suspect that Google wants the correct status code.
Any ideas how I can force the error at this point in WP's loop?
Related Questions
Sponsored Content
2 Answered Questions
Wordpress Permlinks 404 Not Found
- 2013-11-01 14:56:44
- enfany
- 862 View
- 0 Score
- 2 Answer
- Tags: permalinks 404-error localhost
2 Answered Questions
[SOLVED] Getting soft 404 errors (200 status) when caching plugins are enabled
- 2011-03-22 21:59:58
- berberich
- 1896 View
- 2 Score
- 2 Answer
- Tags: cache 404-error plugin-w3-total-cache http
2 Answered Questions
1 Answered Questions
Getting a Error 404 Not Found on subcategory
- 2018-03-06 12:16:31
- Jie Jenn
- 267 View
- 0 Score
- 1 Answer
- Tags: 404-error
3 Answered Questions
[SOLVED] Categories not working - 404 Not found error
- 2011-01-23 00:19:53
- Abhishek Gupta
- 1755 View
- 1 Score
- 3 Answer
- Tags: categories url-rewriting 404-error
1 Answered Questions
[SOLVED] 404 not found error on pagination
- 2013-09-23 10:53:00
- IFightCode
- 6254 View
- 1 Score
- 1 Answer
- Tags: pagination 404-error
2 Answered Questions
[SOLVED] Page not found (404 Error) even if the page exists?
- 2013-10-06 21:08:46
- user25893
- 9438 View
- 0 Score
- 2 Answer
- Tags: 404-error
1 comments
@Matthew Brown aka Lord Matt 2019-07-09 00:16:50
The short version is that you will need to do the lookup much sooner. As pointed out in comments, a 404 status can only be sent with the headers.
There are a few ways that you can solve this issue. I would do it with a class. That class would use a few hooks but most importantly, one would be early on, say "init", for example. That would be when I would have it figure out if it will be needed and do the lookup. That result, I would store in a protected variable.
Then, when it comes time to output the data, I could have another function in the class (called a method) just deal with the output (as the lookup already happened).
This would allow you to respond 404 if the lookup failed long before any content is sent.
There are other ways using hooks and global variables but I prefer classes.
If you need help getting to grips with hooks ask about them here.
If you want help understanding classes, that's Stack Exchange.
If this answer is not helpful enough, another one will be along sooner or later.