There was an issue with the code originally posted by kaiser in that it queried for year, month and day distinctly resulting in values representing “the largest month for any date” or the “smallest day for any date” rather than values representing the MAX
and MIN
dates globally.
I altered that original code to query for globally MAX
and MIN
dates and with kaiser’s blessing am posting it here. The following returns the correct dates when tested on my server but testing has not been by any means exhaustive.
function get_copyright_daterange_v2()
{
global $wpdb;
static $range = array();
if ( ! empty( $range ) )
return $range;
return $range = array_shift( $wpdb->get_results( $wpdb->prepare( "
SELECT
MIN( post_date ) AS first,
MAX( post_date ) AS last
FROM
{$wpdb->posts}
WHERE
post_type = %s
AND NOT post_status="auto-draft"
AND NOT post_status="inherit"
AND NOT post_status="trash"
",
is_admin() ? get_current_screen()->post_type : get_queried_object()->post_type
) ) );
}
$date_range = get_copyright_daterange_v2();
printf(
'© %s — %s',
date_i18n(
get_option( 'date_format' ),
strtotime( $date_range->first ),
true // GMT
),
date_i18n(
get_option( 'date_format' ),
strtotime( $date_range->last ),
true // GMT
)
);