Wordpress

Display WordPress Archive Lists By Specific Year or Month

Oleh takien• 5 Komentar Arsip

WordPress archive by specific date

WordPress has wp_get_archives function to display date-based archives list. Here is the default args for wp_get_archives:

$args = array(
'type' => 'monthly',
'limit' => ,
'format' => 'html',
'before' => ,
'after' => ,
'show_post_count' => false,
'echo' => 1
);

I can choose whether archive should be displayed yearly, monthly, daily or weekly by passing type argument.

The Problem

Unfortunately I ‘CAN NOT’ filter archive only on specific date, in this case I want to display monthly archive on year 2011. I have googled this problem but no luck.

The solution

Then I went into the code in the file where the wp_get_archives function is located, and found this filer hook ‘$where = apply_filters( 'getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );

That is the filter I was looking for, so I created this callback.

function takien_archive_where($where,$args){
$year = isset($args['year']) ? $args['year'] : ";
$month = isset($args['month']) ? $args['month'] : ";
$monthname = isset($args['monthname']) ? $args['monthname']: ";
$day = isset($args['day']) ? $args['day'] : ";
$dayname = isset($args['dayname']) ? $args['dayname'] : ";
if($year){
$where .= " AND YEAR(post_date) = '$year' ";
$where .= $month ? " AND MONTH(post_date) = '$month' " : ";
$where .= $day ? " AND DAY(post_date) = '$day' " : ";
}

if($month){
$where .= " AND MONTH(post_date) = '$month' ";
$where .= $day ? " AND DAY(post_date) = '$day' " : ";
}

if($monthname){

$where .= " AND MONTHNAME(post_date) = '$monthname' ";

$where .= $day ? " AND DAY(post_date) = '$day' " : ";

}

if($day){

$where .= " AND DAY(post_date) = '$day' ";

}

if($dayname){

$where .= " AND DAYNAME(post_date) = '$dayname' ";

}

return $where;

}

Now, I have additional arguments than can be passed to wp_get_archives function:

  • year (string), year to display, eg 2011
  • month (string), monthnum to display, eg 2 for February
  • monthname (string), month name to display, eg January
  • day (string), day of month to display, eg 9
  • dayname (string), day name to display, eg Sunday

Usage

//This is example to display archive list on year 2010.

//call the filter early before any output, the best place is in functions.php

add_filter( 'getarchives_where','takien_archive_where',10,2);

/* place the following code at where output to be displayed.*/

//display the archive list monthly based, on year 2010

//set the arguments

$args = array(

'type' => 'monthly',

'echo' => 0,

'year' => '2010'

);

//render the output

echo '

Monthly archive 2010

';

echo '<ul>'.wp_get_archives($args).'</ul>';

//display the archive list daily based, on February 2011

$args = array(

'type' => 'daily',

'echo' => 0,

'year' => '2011',

'month' => '12'

);

echo '

Daily archive December, 2011

';

echo '<ul>'.wp_get_archives($args).'</ul>';

//set the arguments

$args = array(

'type' => 'daily',

'echo' => 0,

'month' => '1'

);

echo '

Daily archive January, all years

';

echo '<ul>'.wp_get_archives($args).'</ul>';

If there’s post on that specific date, the output result should look like this:

In fact, you may add other MySQL date-time function to the arguments, see this for reference.

Good luck.

Tulisan ini sudah berumur: 14 tahun 7 bulan 2 hari. Informasi di dalamnya mungkin sudah tidak relevan, tapi nilai historisnya sangat berharga, terutama bagi saya sebagai penulisnya.

(Dipulihkan dari takien.com)

Komentar (5)

Arsip Statis
Appearance Hehe Awaludin
Numpang insert ke database gan.. :hammer:
Dodol
alert(“XSS is posible”);
pawel
Thanks for sharing this filter ( :
goldaccess
Great post – helped me a lot today
Nick
A clean solution! Wondering if you could expand on where to setup and call the callback? For instance, can I add this to the functions.php file then call it in the apply_filters() function? I’ve found wp_get_archives() in wp-includes/general-template.php but still new to WordPress and unsure how to implement. Looking forward to hearing back!