WordpressプラグインのACFを利用しカスタムフィールドに日付を登録しました。
登録した日付をアーカイブ一覧で表示させているのですが、値が空だと0年00月と表示されてしまい困っています。
各サイトで公開されている情報を基にfunctions.phpに下記を追加しました。
//カスタムフィールドの値で月別アーカイブ function my_get_month_archives( $args = '' ) { global $wpdb, $wp_locale; $defaults = array( 'date_field' => '開催日', //カスタムフィールドのフィールド名を記述 'format' => 'html', 'echo' => true, 'limit' => '', 'before' => '', 'after' => '', 'show_post_count' => true, ); $r = wp_parse_args( $args, $defaults ); extract( $r, EXTR_SKIP ); if ( '' != $limit ) { $limit = absint( $limit ); $limit = ' LIMIT '.$limit; } $field = 'm.meta_value'; $select = "SELECT SUBSTRING($field,1,4) AS `year`, SUBSTRING($field,5,2) AS `month`, count(p.ID) AS posts"; $where = "WHERE p.post_type = 'post' AND p.post_status = 'publish'"; $where .= $wpdb->prepare( ' AND m.meta_key = %s', $date_field ); $join = " INNER JOIN $wpdb->postmeta AS m ON m.post_id = p.ID"; $where = apply_filters( 'getarchives_where', $where, $r ); $join = apply_filters( 'getarchives_join' , $join , $r ); $output = ''; $query = "$select FROM $wpdb->posts AS p $join $where GROUP BY SUBSTRING($field,1,4), SUBSTRING($field,5,2) ORDER BY $field DESC $limit"; $key = md5( $query ); $cache = wp_cache_get( 'my_get_month_archives' , 'general' ); if ( !isset( $cache[ $key ] ) ) { $arcresults = $wpdb->get_results( $query ); $cache[ $key ] = $arcresults; wp_cache_set( 'my_get_month_archives', $cache, 'general' ); } else { $arcresults = $cache[ $key ]; } if ( $arcresults ) { $afterafter = $after; foreach ( (array) $arcresults as $arcresult ) { $url = add_query_arg( array( 'meta_key' => $date_field ), get_month_link( $arcresult->year, $arcresult->month) ); $text = sprintf( '%d', $arcresult->year ).'年'.sprintf( '%02d, $arcresult->month ).'月'; $output .= '<option value="'.$url.'">'.$text.'</option>'; } } if ( $echo ) echo $output; else return $output; }
そして出力したい場所に下記を追加しました。
<select name="archive-dropdown" onChange='document.location.href=this.options[this.selectedIndex].value;'> <option value="">年代から絞り込む</option> <?php my_get_month_archives( array( 'date_field' => 'release', 'show_post_count' => 1 ) ); ?> </select>
foreachを行う前にarray_filter()やarray_diff()関数を使用して空の配列要素の削除を試したのですが上手くいきません。
※ 0年00月が消えない。
具体的に、どこをどのように書き換えれば良いのか教えて頂けると大変助かります。
よろしくお願いします。

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。