I have a code that I currently use; however, I need more customization.
$latestarchive = wp_get_archives([
"type" => "monthly",
"limit" => "1",
"echo" => 0,
"order" => "DESC"]);
echo $latestarchive;
This produced:
<a href="https://gp-cpdev/2021/04/">April 2021</a>
However, is there a way I can modify it in such a way as to produce the following code…
<a href="https://gp-cpdev/2021/04/">Home</a>
Or to output a year/month string such as 2021/04
?
I would even want to display only a text such as “April 2021” that is not a link—useful for pagination by month. Here’s an example (note that “April 2021” will not be a link:
Previous month April 2021 Next month
Now if I could set a custom text for the latest year/month, I could figure out the pagination by month and go from there. My thinking is that the archives will be listed in an array format instead of outputting as HTML code. So then I could determine if it’s the first month or the last month in an array by determining an index of an array. Something like this:
$currentMonth = array_search("2021/04", $archiveByMonth); // Returns 1; "2021/04" would be from a URL.
if($currentMonth > 0)
// Display the "Previous Month" link.
if($currentMonth < count($archiveByMonth)
// Display the "Next Month" link.
So yeah, I think what I want is something similar to wp_get_archives($args)
but for outputting as an array instead of HTML. I don’t care for the limit as I can get the string from the upper bound of an array.
After I did some digging, I started to think I need to write a custom function. I came across this web page:
https://core.trac.wordpress.org/browser/tags/4.1.1/src/wp-includes/general-template.php#L1321
And I modified the line of code to something like this:
function get_monthly_archive_array() {
global $wpdb;
$r['type'] = 'monthly';
$where = apply_filters( 'getarchives_where',
"WHERE post_type = 'post' AND post_status = 'publish'", $r );
$last_changed = wp_cache_get( 'last_changed', 'posts' );
if ( ! $last_changed ) {
$last_changed = microtime();
wp_cache_set( 'last_changed', $last_changed, 'posts' );
}
/**
* Filter the SQL JOIN clause for retrieving archives.
*
* @since 2.2.0
*
* @param string $sql_join Portion of SQL query containing JOIN clause.
* @param array $r An array of default arguments.
*/
$join = apply_filters( 'getarchives_join', '', $r );
$query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date ASC";
$key = md5( $query );
$key = "wp_get_archives:$key:$last_changed";
if ( ! $results = wp_cache_get( $key, 'posts' ) ) {
$results = $wpdb->get_results( $query );
wp_cache_set( $key, $results, 'posts' );
}
if ( $results ) {
return (array)$results;
}
}
Is this the right approach or is there a similar function that would output as an array?
I am using my own custom theme, so I would like to avoid plugins whenever I can. I do know some SQL and I have experience with PHP, if that helps. So how can I go about setting a custom text as described above or when getting only the custom-formatted year and month for a URL?
UPDATE as of 2001/04/29 11:05 AM: I think I got it! The function I made from the wordpress.org webpage exposes the array such as this:
array(2) { [0]=> object(stdClass)#605 (3) { ["year"]=> string(4) "2021" ["month"]=> string(1) "2"
["posts"]=> string(1) "6" } [1]=> object(stdClass)#606 (3) { ["year"]=> string(4) "2021" ["month"]=>
string(1) "4" ["posts"]=> string(1) "2" } }
I believe I have answered my question. I then can go ahead and format the year and month to my heart’s content. Although the code from the WordPress website does not have a lot of comments describing how this all works including $key = "wp_get_archives:$key:$last_changed";
.
UPDATE as of 11:13 AM: I also added the $join
code. Maybe that’s needed as well.