サポート » 使い方全般 » カスタムフィールドの値で年別アーカイブ

  • カスタムフィールドの値で年別アーカイブを導入しましたが一部、カスタムフィールドの値が空の記事が
    あり、その為 select で年度別に表示させると下記の様になってしまいます。

    
    <select class="form-control" name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;"> 
     <option value="">年代から絞り込む</option>
     <option value="2019" selected="">2019年</option>
     <option value="2018">2018年</option>
     <option value="2016">2016年</option>
     <option value="2014">2014年</option>
     <option value="2012">2012年</option>
     <option value="2011">2011年</option>
     <option value="2010">2010年</option>
     <option value="2009">2009年</option>
     <option value="2008">2008年</option>
     <option value="2006">2006年</option>
     <option value="2019">0年</option>
    </select>
    

    配列の一部[year]が空の場合は排除(削除)する方法が分かりません。

    
    object(stdClass)#7626 (2) {
      ["year"]  => string(0) ""
      ["posts"] => string(1) "1"
    }
    

    全体のコードは下記になります。

    
    <?php
    function my_get_year_archives( $args = '' ) {
        global $wpdb, $wp_locale;
        $defaults = array(
            'date_field' => 'release_date', //カスタムフィールドのフィールド名を記述
            '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 <code>year</code>, count(p.ID) AS posts";
        $where  = "WHERE p.post_type = 'comic' 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) ORDER BY $field DESC $limit";
        $key = md5( $query );
        $cache = wp_cache_get( 'my_get_year_archives' , 'general' );
        if ( !isset( $cache[ $key ] ) ) {
            $arcresults = $wpdb->get_results( $query );
            $cache[ $key ] = $arcresults;
            wp_cache_set( 'my_get_year_archives', $cache, 'general' );
        } else {
            $arcresults = $cache[ $key ];
        }
    
       if ( $arcresults ) {
            $defAfter = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
            $After = preg_replace('/[^0-9]/', '', $defAfter);
    
            foreach ( (array) $arcresults as $arcresult ) {
    var_dump($arcresults);
                $After = substr($defAfter, 29, 4); // URLから年4桁を抜き出す
                $Years = $arcresult->year;
                $selected = $Years === $After ? ' selected' : '';
                $url = add_query_arg( array( 'meta_key' => $date_field ), get_year_link( $arcresult->year ) );
                $text = sprintf( '%d', $arcresult->year ).'年';
    			$output .= '<option value="'.$url.'"'.$selected.'>'.$text.'</option>';
            }
        }       
        if ( $echo )
            echo $output;
        else
            return $output;
    }
    ?>
    

    どのように対処すればいいか教えて頂けないでしょうか?
    どうか宜しくお願いします。

    ヘルプの必要なページ: [リンクを見るにはログイン]

  • トピック「カスタムフィールドの値で年別アーカイブ」には新たに返信することはできません。