Stick this in your theme’s functions.php
add_action( 'restrict_manage_posts' , 'add_my_year_input' );
function add_my_year_input() {
// Get the date query that already exists, avoids the need for an extra one
global $arc_result;
// Create empty array
$archive_years = array();
// Loop over results plucking out the years
foreach( (array) $arc_result as $key => $date_object ) {
$archive_years[] = $date_object->yyear;
}
// Clear out the duplicates
$archive_years = array_unique( $archive_years );
// Make sure we have numeric values
$archive_years = array_map( 'intval' , $archive_years );
// Check we have years in the array
if( !empty( $archive_years ) ) {
$current_year = get_query_var( 'year' );
// Start building the select box
echo '<select name="year">';
// Add empty default option
echo '<option value="">Show all years</option>';
// Loop over the years adding them as options
foreach( $archive_years as $yearr ) {
// Check if the year is set already and set this to current if so
if( $current_year && $yearr == (int) $current_year )
echo "<option selected=\"selected\" value=\"$yearr\">$yearr</option>";
// Else just print out as regular option
else
echo "<option value=\"$yearr\">$yearr</option>";
}
// Finish the select
echo '</select>';
}
// Nothing left to do, return
return;
}
Tested under 2.9.2 using the default theme.
Hi t31os_,
Due to the project I only had the time to test this script today. It works perfectly. the function $arc_result can it also filter on a specific day?
Thanks so far for your help btw!
$arc_result
Is a variable from WordPress, it happens to be available for the context of the code above, i can’t remember any specifics about what it contains (without going to look).
Plonk this after the global line for a few moments..
print '<pre>';
print_r( $arc_result );
print '</pre>';
Might not want to do that on a production site, but it’s obviously fine inside a test environment. It will give you a print out of what’s in the variable.