On a date archive page when no posts exist (for that date) WordPress throws across to the 404 page.
Is there a way to make WordPress continue to use the original archive instead of the 404. That way I can use if ( have_posts() )
condition to output a “No posts found” message.
You can move the code from OP’s answer into a 404 template filter and force WP to load the
date.php
file instead of404.php
. It’ll still send the 404 header, but render the page with a different template.Okay, on reflection I’m not sure if I’m asking too much of WP. I guess from an SEO point of view I do want empty pages to 404, I certainly don’t want them indexed.
I decided instead to use the 404 template to output the “No posts found” message, but because the functions
is_date()
,is_day()
(et al) will not work on the 404 page I added this code to detect if the 404 was meant to be a date archive.Paste the following into your
404.php
to detect the URL:I can then use the variable
$is_date_archive
to modify the 404 template accordingly.Example (
404.php
)