Wordpressのカスタムフィールド(日付)で、カスタム投稿の記事一覧(archive-***.php)をソートしたい
selectボックスを用いて、投稿タイプを年別(カスタムフィールドで入力)にソートしたいです
ネットで見つけたコードをコピペし、適宜変更しましたが、以下の点が上手くいかないので、コードの間違いを教えていただけると嬉しいです
####要点
1.年別アーカイブページのURLが正しくならない
(カスタム投稿アーカイブページ ***.com/wp/event/)
↓ ソート
現在の年別ソートURL ***.com/wp/2020/?meta_key=event_date
こうしたいURL .com/wp/event/2020/
2.フィールドによるアーカイブページ(年別)が作られない
該当のソースコード
functions.php
php
1/カスタムフィールドの値で月別アーカイブ 2function my_get_year_archives( $args = '' ) { 3 global $wpdb, $wp_locale; 4 5 $defaults = array( 6 'date_field' => 'event_date', //カスタムフィールドのフィールド名を記述 7 'format' => 'html', 8 'echo' => true, 9 'limit' => '', 10 'before' => '', 11 'after' => '', 12 'show_post_count' => false, 13 ); 14 15 $r = wp_parse_args( $args, $defaults ); 16 extract( $r, EXTR_SKIP ); 17 18 if ( '' != $limit ) { 19 $limit = absint( $limit ); 20 $limit = ' LIMIT '.$limit; 21 } 22 23 $field = 'm.meta_value'; 24 $select = "SELECT SUBSTRING($field,1,4) AS `year`, count(p.ID) AS posts"; 25 $where = "WHERE p.post_type = 'event' AND p.post_status = 'publish'"; 26 $where .= $wpdb->prepare( ' AND m.meta_key = %s', $date_field ); 27 $join = " INNER JOIN $wpdb->postmeta AS m ON m.post_id = p.ID"; 28 29 $where = apply_filters( 'getarchives_where', $where, $r ); 30 $join = apply_filters( 'getarchives_join' , $join , $r ); 31 32 $output = ''; 33 $query = "$select FROM $wpdb->posts AS p $join $where GROUP BY SUBSTRING($field,1,4) ORDER BY $field DESC $limit"; 34 $key = md5( $query ); 35 $cache = wp_cache_get( 'my_get_year_archives' , 'general' ); 36 if ( !isset( $cache[ $key ] ) ) { 37 $arcresults = $wpdb->get_results( $query ); 38 $cache[ $key ] = $arcresults; 39 wp_cache_set( 'my_get_year_archives', $cache, 'general' ); 40 } else { 41 $arcresults = $cache[ $key ]; 42 } 43 if ( $arcresults ) { 44 $afterafter = $after; 45 foreach ( (array) $arcresults as $arcresult ) { 46 $url = add_query_arg( array( 'meta_key' => $date_field ), get_year_link( $arcresult->year) ); 47 $text = sprintf( '%d', $arcresult->year ).'年'; 48 $output .= '<option value="'.$url.'">'.$text.'</option>'; 49 } 50 } 51 if ( $echo ) 52 echo $output; 53 else 54 return $output; 55} 56 57add_action( 'init', 'my_init' ); 58function my_init() { 59 global $wp; 60 $wp->add_query_var( 'meta_key' ); 61} 62function change_sort_order( $query ) { 63 if ( is_admin() || ! $query->is_main_query() ) { 64 return; 65 } 66 $currnet_date = date_i18n( 'y/m/d' ); 67 if ( $query->is_year() ) { 68 $meta_query = array( 69 array( 70 'key' => $query->get( 'meta_key' ), 71 'value' => $query->get( 'year' ).'.'.sprintf('%02d', $query->get( 'monthnum' )), 72 'compare' => 'LIKE' 73 ), 74 ); 75 $thisyear = get_query_var('year'); 76 $thismon = get_query_var('monthnum'); 77 $query->set( 'meta_query', $meta_query ); 78 $query->set( 'year', ''); 79 $query->set( 'monthnum',''); 80 $query->set( 'thisyear',$thisyear); 81 $query->set( 'thismon',$thismon); 82 $query->set( 'orderby', 'meta_value' ); 83 $query->set( 'meta_key','event_date'); 84 $query->set( 'post_type','event'); 85 } 86} 87add_action( 'pre_get_posts', 'change_sort_order' );
補足情報(FW/ツールのバージョンなど)
Wordpress5.2
あなたの回答
tips
プレビュー