By David Snow


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?

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.

Related Questions

Sponsored Content

2 Answered Questions

Wordpress Permlinks 404 Not Found

2 Answered Questions

2 Answered Questions

[SOLVED] 404 Not found error after update to WordPress 5.0

  • 2018-12-19 07:45:36
  • ehsan
  • 329 View
  • 0 Score
  • 2 Answer
  • Tags:   errors 404-error

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

1 Answered Questions

[SOLVED] 404 not found error on pagination

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

Sponsored Content