2

I'd like to find an optimized, WP_Query-safe method for generating a dynamically-populated copyright statement for the bottom of my themes. That is to say, I'd like to check the date (by year) of my oldest and newest posts and then output something along the lines of

[blog name] © [oldest post year]-[newest post year] [primary blog author]

What's the easiest/safest way to do so?

1 Answer 1

2

Here's what I use:

function oenology_copyright() {
    global $wpdb;
    $copyright_dates = $wpdb->get_results("
        SELECT
            YEAR(min(post_date_gmt)) AS firstdate,
            YEAR(max(post_date_gmt)) AS lastdate
        FROM
            $wpdb->posts
        WHERE
            post_status = 'publish'
    ");
    $output = '';
    if($copyright_dates) {
        $copyright = "© " . $copyright_dates[0]->firstdate;
            if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
                $copyright .= '-' . $copyright_dates[0]->lastdate;
            }
        $output = $copyright;
    }
    return $output;
}

Of course, if there's a cleaner, safer, or more efficient method, I'd love to hear about it, too!

EDIT:

And here's a more cleaned-up version, that adds the copyright dates to wp_cache:

function oenology_copyright() {
    // check for cached values for copyright dates
    $copyright_cache = wp_cache_get( 'copyright_dates', 'oenology' );
    // query the database for first/last copyright dates, if no cache exists
    if ( false === $copyright_cache ) { 
        global $wpdb;
        $copyright_dates = $wpdb->get_results("
            SELECT
            YEAR(min(post_date_gmt)) AS firstdate,
            YEAR(max(post_date_gmt)) AS lastdate
            FROM
            $wpdb->posts
            WHERE
            post_status = 'publish'
        ");
        $copyright_cache = $copyright_dates;
        // add the first/last copyright dates to the cache
        wp_cache_set( 'copyright_dates', $copyright_cache, 'oenology', '604800' );
    }
    // Build the copyright notice, based on cached date values
    $output = '© ';
    if( $copyright_cache ) {
        $copyright = $copyright_cache[0]->firstdate;
        if( $copyright_cache[0]->firstdate != $copyright_cache[0]->lastdate ) {
            $copyright .= '-' . $copyright_cache[0]->lastdate;
        }
        $output .= $copyright;
    } else {
        $output .= date( 'Y' );
    }
    return $output;
}

Would this improve performance a bit?

3
  • One way I can think of improving this is too save the results in a transient for the remaining days of the year. Not sure how much your query is in terms of performance, and its acting on large installations, caching might be a good idea.
    – Ashfame
    Commented Apr 11, 2011 at 16:08
  • This is indeed close and the route I was thinking of taking, though the additional query aspect is something that I'm hoping can be sidestepped by some other enterprising soul.
    – ZaMoose
    Commented Apr 11, 2011 at 16:11
  • I just added a revised function, that uses the wp_cache (wp_cache_get(), wp_cache_set()) to store/retrieve the copyright date values. Commented Apr 22, 2011 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.