Help us understand the problem. What is going on with this article?

【wordpress】タームを指定した年別アーカイブリストを出力する

More than 1 year has passed since last update.

カスタム投稿の特定のタームに絞った年別アーカイブリストの出し方です。

なお、「【wordpress】月別アーカイブの作り方」はこちら

■ functions.php

functions.phpに以下、記述。

functions.php
/*
 * 出力例
 * $args = array(
 *  'type'            => 'yearly',
 *  'format'          => 'html',
 *  'show_post_count' => true,
 *  'post_type'       => '{カスタム投稿名}',
 *  'taxonomy'        => '{タクソノミー名}',
 *  'slug'            => '{ターム名}'
 * );
 * wp_get_archives( $args );
 */

add_filter( 'getarchives_join', 'my_getarchives_join', 10, 2 );

function my_getarchives_join( $join, $r ) {
  global $wpdb;

  if( isset( $r['taxonomy']) ) {
     $join .= "LEFT JOIN $wpdb->term_relationships ON ( $wpdb->posts.ID = $wpdb->term_relationships.object_id )";

    $join .= "LEFT JOIN $wpdb->term_taxonomy ON ( $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id )";

    $join .= "LEFT JOIN $wpdb->terms ON ( $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id )";
  }
  return $join;
}

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

function my_getarchives_where( $where, $r ) {
  global $wpdb;

  if( isset( $r['taxonomy']) ) {
    $where .= $wpdb->prepare( "AND $wpdb->term_taxonomy.taxonomy = %s", $r['taxonomy'] );
  }

  if( isset( $r['slug'] ) ) {
    $where .= $wpdb->prepare( "AND $wpdb->terms.slug = %s", $r['slug'] );
  }
  return $where;
}

■ テンプレート

テンプレートファイルに以下、記述。

archive.php
<ul>
  <?php
    $args = array(
      'type'            => 'yearly',
      'format'          => 'html',
      'show_post_count' =>  true,
      'post_type'       => '{カスタム投稿名}',
      'taxonomy'        => '{タクソノミー名}',
      'slug'            => '{ターム名}'
    );
    wp_get_archives( $args );
  ?>
</ul>

以下のようなコードが出力されます。

<ul>
  <li><a href="ドメイン/{カスタム投稿名}/date/2018/">2018</a>(2)</li>
  <li><a href="ドメイン/{カスタム投稿名}/date/2017/">2017</a>(7)</li>
  <li><a href="ドメイン/{カスタム投稿名}/date/2016/">2016</a>(2)</li>
  <li><a href="ドメイン/{カスタム投稿名}/date/2015/">2015</a>(1)</li>
</ul>

参考:【WordPress】カスタム投稿でタクソノミーのタームごとに月別アーカイブをなんとか表示させる方法。

m_t_of
wordpressのカスタマイズ制作をしていることが多いです。 自分用のスニペット集代わりに記事を書いていこうと思います。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした