{"id":168328,"date":"2013-11-04T08:33:28","date_gmt":"2013-11-03T23:33:28","guid":{"rendered":""},"modified":"2016-07-30T20:33:41","modified_gmt":"2016-07-30T20:33:41","slug":"%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%a0%e3%83%95%e3%82%a3%e3%83%bc%e3%83%ab%e3%83%89%e3%81%ab%e3%82%88%e3%82%8b%e3%82%a2%e3%83%bc%e3%82%ab%e3%82%a4%e3%83%96%e3%83%aa%e3%82%b9%e3%83%88%e3%81%ab%e3%81%a4","status":"closed","type":"topic","link":"https:\/\/ja.wordpress.org\/support\/topic\/%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%a0%e3%83%95%e3%82%a3%e3%83%bc%e3%83%ab%e3%83%89%e3%81%ab%e3%82%88%e3%82%8b%e3%82%a2%e3%83%bc%e3%82%ab%e3%82%a4%e3%83%96%e3%83%aa%e3%82%b9%e3%83%88%e3%81%ab%e3%81%a4\/","title":{"rendered":"\u30ab\u30b9\u30bf\u30e0\u30d5\u30a3\u30fc\u30eb\u30c9\u306b\u3088\u308b\u30a2\u30fc\u30ab\u30a4\u30d6\u30ea\u30b9\u30c8\u306b\u3064\u3044\u3066"},"content":{"rendered":"<p>kz\u69d8\u304c\u66f8\u304b\u308c\u3066\u3044\u308b\u30b3\u30fc\u30c9\u3067\u30ab\u30b9\u30bf\u30e0\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u5024\u3067\u30a2\u30fc\u30ab\u30a4\u30d6\u4e00\u89a7\u3092\u8868\u793a\u3055\u305b\u3088\u3046\u3068<br \/>\n\u8a66\u307f\u307e\u3057\u305f\u304c\u4e0a\u624b\u304f\u52d5\u4f5c\u305b\u305a\u56f0\u3063\u3066\u304a\u308a\u307e\u3059\u3002<\/p>\n<p><a href=\"http:\/\/wpxtreme.jp\/yearly-archive-using-custom-field-in-wordpress\">\u30ab\u30b9\u30bf\u30e0\u30d5\u30a3\u30fc\u30eb\u30c9\u306b\u3088\u308b\u30a2\u30fc\u30ab\u30a4\u30d6\u30ea\u30b9\u30c8\u306b\u3064\u3044\u3066<\/a><\/p>\n<p>functions.php\u306b\u30b3\u30fc\u30c9\u3092\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\u3057\u30a6\u30a3\u30b8\u30a7\u30c3\u30c8\u306b\u30a2\u30fc\u30ab\u30a4\u30d6\u3092\u8868\u793a\u3055\u305b\u308b\u30b3\u30fc\u30c9\u3092<br \/>\n\u8a18\u5165\u3059\u308b\u3068\u4e00\u89a7\u8868\u793a\u306f\u3055\u308c\u307e\u3059\u304c\uff08\u5024\u306e\u6570\u5b57\u306e\u307f\uff09\u30af\u30ea\u30c3\u30af\u3059\u308b\u3068\u30ea\u30f3\u30af\u30a8\u30e9\u30fc(404)\u3068\u306a<br \/>\n\u3063\u3066\u3057\u307e\u3044\u6295\u7a3f\u3057\u305f\u5e74\u306e\u307f\u5168\u3066\u306e\u8a18\u4e8b\u304c\u8868\u793a\u3055\u308c\u308b\u72b6\u614b\u3067\u3059\u3002<\/p>\n<p>functions.php<\/p>\n<pre><code>function my_get_year_archives( $args = &#039;&#039; ) {\n\tglobal $wpdb, $wp_locale;\n\n\t$defaults = array(\n\t\t&#039;date_field&#039; =&gt; &#039;date&#039;,\n\t\t&#039;format&#039; =&gt; &#039;html&#039;,\n\t\t&#039;echo&#039; =&gt; true,\n\t\t&#039;limit&#039; =&gt; &#039;&#039;,\n\t\t&#039;before&#039; =&gt; &#039;&#039;,\n\t\t&#039;after&#039; =&gt; &#039;&#039;,\n\t\t&#039;show_post_count&#039; =&gt; true,\n\t);\n\n\t$r = wp_parse_args( $args, $defaults );\n\textract( $r, EXTR_SKIP );\n\n\tif ( &#039;&#039; != $limit ) {\n\t\t$limit = absint( $limit );\n\t\t$limit = &#039; LIMIT &#039;.$limit;\n\t}\n\n\t$field  = &#039;m.meta_value&#039;;\n\t$select = &quot;SELECT SUBSTRING($field,1,4) AS &lt;code&gt;year&lt;\/code&gt;, count(p.ID) AS posts&quot;;\n\t$where  = &quot;WHERE p.post_type = &#039;post&#039; AND p.post_status = &#039;publish&#039;&quot;;\n\t$where .= $wpdb-&gt;prepare( &#039; AND m.meta_key = %s&#039;, $date_field );\n\t$join   = &quot; INNER JOIN $wpdb-&gt;postmeta AS m ON m.post_id = p.ID&quot;;\n\n\t$where  = apply_filters( &#039;getarchives_where&#039;, $where, $r );\n\t$join   = apply_filters( &#039;getarchives_join&#039; , $join , $r );\n\n\t$output = &#039;&#039;;\n\t$query = &quot;$select FROM $wpdb-&gt;posts AS p $join $where GROUP BY SUBSTRING($field,1,4) ORDER BY $field DESC $limit&quot;;\n\t$key = md5( $query );\n\t$cache = wp_cache_get( &#039;my_get_year_archives&#039; , &#039;general&#039; );\n\tif ( !isset( $cache[ $key ] ) ) {\n\t\t$arcresults = $wpdb-&gt;get_results( $query );\n\t\t$cache[ $key ] = $arcresults;\n\t\twp_cache_set( &#039;my_get_year_archives&#039;, $cache, &#039;general&#039; );\n\t} else {\n\t\t$arcresults = $cache[ $key ];\n\t}\n\n\tif ( $arcresults ) {\n\t\t$afterafter = $after;\n\t\tforeach ( (array) $arcresults as $arcresult ) {\n\t\t\t$url = add_query_arg( array( &#039;meta_key&#039; =&gt; $date_field ), get_year_link( $arcresult-&gt;year ) );\n\t\t\t$text = sprintf( &#039;%d&#039;, $arcresult-&gt;year );\n\t\t\tif ($show_post_count)\n\t\t\t\t$after = &#039;\u00a0(&#039;.$arcresult-&gt;posts.&#039;)&#039; . $afterafter;\n\t\t\t$output .= get_archives_link( $url, $text, $format, $before, $after );\n\t\t}\n\t}\n\n\tif ( $echo )\n\t\techo $output;\n\telse\n\t\treturn $output;\n}\n\nadd_action( &#039;init&#039;, &#039;my_init&#039; );\nfunction my_init() {\n\tglobal $wp;\n\t$wp-&gt;add_query_var( &#039;meta_key&#039; );\n}\n\nadd_action( &#039;pre_get_posts&#039;, &#039;my_pre_get_posts&#039; );\nfunction my_pre_get_posts( $query ) {\n\tif ( $query-&gt;is_year ) {\n\t\t$meta_query = array(\n\t\t\tarray(\n\t\t\t\t&#039;key&#039;     =&gt; $query-&gt;get( &#039;meta_key&#039; ),\n\t\t\t\t&#039;value&#039;   =&gt; $query-&gt;get( &#039;year&#039;     ),\n\t\t\t\t&#039;compare&#039; =&gt; &#039;LIKE&#039;\n\t\t\t),\n\t\t);\n\n        $query-&gt;set( &#039;meta_query&#039; , $meta_query );\n\t\t$query-&gt;set( &#039;year&#039;       , &#039;&#039;          );\n\t\t$query-&gt;set( &#039;date&#039;       , &#039;&#039;          );\n\t\t$query-&gt;set( &#039;meta_key&#039;   , &#039;date&#039;);\n\t\t$query-&gt;set( &#039;orderby&#039;    , &#039;meta_value&#039; );\n\t\t$query-&gt;set( &#039;order&#039;      , &#039;DECS&#039;       );\n\t}\n}<\/code><\/pre>\n<p>\u30a6\u30a3\u30b8\u30a7\u30c3\u30c8<br \/>\n<code>&lt;?php my_get_year_archives( array( &#039;date_field&#039; =&gt; &#039;date&#039;, ) ); ?&gt;<\/code><\/p>\n<p>\u30ab\u30b9\u30bf\u30e0\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u5024(YYYY)\u306f\u53d6\u5f97\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u3053\u308c\u3092\u3069\u306e\u3088\u3046\u306b\u4fee\u6b63\u3057\u305f\u3089<br \/>\n\u826f\u3044\u306e\u304b\u5206\u304b\u3089\u305a\u56f0\u3063\u3066\u3044\u307e\u3059\u3001\u89e3\u6c7a\u65b9\u6cd5\u3092\u304a\u6559\u3048\u9802\u3051\u307e\u3059\u3068\u5e78\u3044\u3067\u3059\u3002<\/p>\n<p>\u203bdate\u306b\u306f\u30ab\u30b9\u30bf\u30e0\u30d5\u30a3\u30fc\u30eb\u30c9\u540d\u3001\u30ab\u30b9\u30bf\u30e0\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u5024\u306b\u306fYYYY\u5e74MM\u6708DD\u65e5\u3068\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"template":"","class_list":["post-168328","topic","type-topic","status-closed","hentry","topic-tag-14401","topic-tag-14005","topic-tag-14327","topic-tag-14430"],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/ja.wordpress.org\/support\/wp-json\/wp\/v2\/topic\/168328","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ja.wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/ja.wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/ja.wordpress.org\/support\/wp-json\/wp\/v2\/topic\/168328\/revisions"}],"wp:attachment":[{"href":"https:\/\/ja.wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=168328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}