How to get post archives urls list without any markup?

I'm using wp_get_archives but I cannot prepend any text inside the links. I want code like this:

a href="archive link"[my custom prepend] 2020a/

But the 'before' option will prepend the passed string before the a tag.

Current code is the following:

$archives = wp_get_archives(array(
  'type' = 'yearly'
  ,'echo' = false
  ,'format' = 'custom'
  ,'before' = 'News'
));

So I really would like to get a list of only years and urls, without any markup, so I can build it by myself. Thus maybe without wp_get_archives().

Maybe the best thing would be have a simple array like:

array(
   array('year' = 2020, 'url' = 'https://www.....')
)

How to?

Topic wp-get-archives archives Wordpress

Category Web


I maneged to do like this:

First, get simply the years:

// this is a multipurpose function I made, and it's quite self explanatory
function get_years($post_type, $order = "ASC", $exclude = array(), $future = true, $taxonomy = null, $term = null){
  global $wpdb;
  $type = ($post_type) ? $post_type : 'post';
  $status = ($future) ? " ('publish', 'future') " : " ('publish') ";
  $exclude = (!empty($exclude)) ? " AND YEAR(post_date) NOT IN (" . implode(',',$exclude) . ") " : "";

  return $wpdb->get_results($wpdb->prepare("
    SELECT YEAR(post_date) AS `y`, count(ID) as posts_count
    FROM $wpdb->posts p
      INNER JOIN $wpdb->term_relationships rel ON p.ID = rel.object_id
      INNER JOIN $wpdb->term_taxonomy tt ON tt.term_taxonomy_id = rel.term_taxonomy_id
      INNER JOIN $wpdb->terms t ON tt.term_id = t.term_id
    WHERE
      p.post_type = %s
      AND tt.taxonomy = %s
      AND t.slug = %s
      AND post_status IN " . $status . "
      $exclude
    GROUP BY y
    ORDER BY post_date " . $order,
    $post_type,
    $taxonomy,
    $term
  ),OBJECT_K);
}

Then, cicle through the years and build the permalinks:

get_year_link($year); // this is wp. 

It works for posts, otherwise one could build the likes in a more 'manual' way:

home_url(user_trailingslashit('write some path here' . $year));

I was going to suggest creating a regex to parse it but if you look at this comment on the Wordpress wp_get_archives() docs it looks like someone has gotten there before us.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.