質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.51%
WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

Q&A

解決済

2回答

8332閲覧

WPの月別アーカイブを複数のカスタム投稿が混在した形で表示したい

MOMONUTS

総合スコア43

WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

0グッド

0クリップ

投稿2017/03/03 01:57

編集2017/03/03 02:03

WPでカスタム投稿を数種類作り、固定ページで複数のカスタム投稿の記事が混在した記事一覧リストを作っております。
記事表示やページ送りは問題なく出来るのですが、複数のカスタム投稿記事を混在させた月別アーカイブが作れず苦戦しております。

固定ページはテンプレートphpを作り、その中に

<?php $wp_query = new WP_Query(); $my_posts03 = array( 'post_type' => array('topics_01', 'topics_02', 'topics_03', 'topics_04', 'topics_05', 'topics_06'), 'posts_per_page'=> '10', 'paged' => $paged, ); $wp_query->query( $my_posts03 ); if( $wp_query->have_posts() ): while( $wp_query->have_posts() ) : $wp_query->the_post(); ?> <ul class="topnews clearfix"> <li><?php the_time('Y年m月d日') ?></li> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> </ul> <?php endwhile; endif; wp_reset_postdata(); ?> <?php get_template_part( 'nav', 'below' ); ?>

と記述し、記事一覧とページ送りを表示させております。

月別アーカイブ部分は

<?php $string = wp_get_archives(array( 'show_post_count' => 1, 'post_type' => array('topics_01', 'topics_02', 'topics_03', 'topics_04', 'topics_05', 'topics_06'), 'echo' => 0 )); echo preg_replace('/<\/a>&nbsp;(\([0-9]*\))/', ' <span class="count">$1</spn></a>', $string); ?>

と記述してみましたが、複数ですと認識されません。
'post_type' =>topics_01,
のみですと正常に動作するのですが...

試行錯誤してみましたが、どの様に記述すれば良いか分からず質問させていただきました。

ご教授お願いいたしますm(_ _)m

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

wp_get_archives()post_typeパラメータは文字列しか受け付けないため、アクションとフィルターに関数をフックして設定する必要があります。

PHP

1 2function my_pre_get_posts( $query ) { 3 if ( $query->is_month() && $query->is_main_query() ) { 4 $query->set( 'post_type', array('post','test','syohin') ); 5 } 6} 7add_action( 'pre_get_posts', 'my_pre_get_posts' ); 8 9function my_getarchives_where( $where ){ 10 $where = "WHERE"; 11 $where .= " (post_type = 'post' OR post_type = 'test' OR post_type = 'shohin')"; 12 $where .= " AND post_status = 'publish'"; 13 return $where; 14} 15add_filter( 'getarchives_where', 'my_getarchives_where' );

###追記
元のwp_get_archives()の仕様をなるべく活かすように改造したら長くなりました。
テストはあまりしていませんが、多分動くと思います。

  • wp_get_archives()の'post_type'パラメータが投稿タイプ名の配列を受け付けるように改造
  • 年月アーカイブページで複数の投稿タイプを表示できるようにクエリを追加

####アーカイブリストを表示

PHP

1<?php 2$args = array( 3 'type' => 'monthly', 4 'limit' => '', 5 'format' => 'html', 6 'before' => '', 7 'after' => '', 8 'show_post_count' => false, 9 'echo' => 1, 10 'post_type' => array('post', 'page'), 11); 12my_wp_get_archives( $args ) 13?>

####functions.phpに記載

PHP

1function my_query_vars( $vars ){ 2 $vars[] = "apt"; 3 return $vars; 4} 5add_filter( 'query_vars', 'my_query_vars' ); 6 7function my_pre_get_posts($query) { 8 if ( !is_admin() && $query->is_main_query() ) { 9 if ($query->is_archive) { 10 $apt = $query->get('apt'); 11 12 if( !empty($apt) ) { 13 $post_type_array = explode(',', $apt); 14 $query->set( 'post_type', $post_type_array ); 15 } 16 } 17 } 18} 19add_action( 'pre_get_posts','my_pre_get_posts' ); 20 21function my_wp_get_archives( $args = '' ) { 22 global $wpdb, $wp_locale; 23 24 $defaults = array( 25 'type' => 'monthly', 'limit' => '', 26 'format' => 'html', 'before' => '', 27 'after' => '', 'show_post_count' => false, 28 'echo' => 1, 'order' => 'DESC', 29 'post_type' => 'post' 30 ); 31 32 $r = wp_parse_args( $args, $defaults ); 33 34 if( !is_array($r['post_type']) ) { 35 $r['post_type'] = array($r['post_type']); 36 } 37 38 foreach( $r['post_type'] as $post_type ) { 39 $post_type_object = get_post_type_object( $post_type ); 40 if ( ! is_post_type_viewable( $post_type_object ) ) { 41 return; 42 } 43 $post_types[] = $post_type_object->name; 44 } 45 46 $r['post_type'] = $post_types; 47 48 if ( '' == $r['type'] ) { 49 $r['type'] = 'monthly'; 50 } 51 52 if ( ! empty( $r['limit'] ) ) { 53 $r['limit'] = absint( $r['limit'] ); 54 $r['limit'] = ' LIMIT ' . $r['limit']; 55 } 56 57 $order = strtoupper( $r['order'] ); 58 if ( $order !== 'ASC' ) { 59 $order = 'DESC'; 60 } 61 62 // this is what will separate dates on weekly archive links 63 $archive_week_separator = '&#8211;'; 64 65 $sql_where = $wpdb->prepare("WHERE (post_type = %s", $r['post_type'][0]); 66 if( !(count($r['post_type'])==1) ) { 67 for($i=1; $i<count($r['post_type']); $i++) { 68 $sql_where .= $wpdb->prepare(" OR post_type = %s", $r['post_type'][$i]); 69 } 70 } 71 $sql_where .= ") "; 72 73 $sql_where .= "AND post_status = 'publish'"; 74 75 $where = $sql_where; 76 $join = ''; 77 78 $output = ''; 79 80 $last_changed = wp_cache_get_last_changed( 'posts' ); 81 82 $limit = $r['limit']; 83 84// 文字数制限に引っかかったので、続きは別回答に記載

投稿2017/03/06 07:24

編集2017/03/09 09:08
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

MOMONUTS

2017/03/07 00:16

miz様 ご回答ありがとうございます。 教えていただいたコードをfunctionsに追記する事で全カテゴリー記事が混在した月別アーカイブ表示が出来ました。 ただし、やりたい事は topics01の記事一覧ページ=topics01のみの月別アーカイブを表示 topics02の記事一覧ページ=topics02のみの月別アーカイブを表示 topics03の記事一覧ページ=topics03のみの月別アーカイブを表示 . . . 全カテゴリー記事が混在した記事一覧ページ(固定ページ)=全カテゴリーの記事が混在した月別アーカイブを表示 という形になります。 特定のページのみ全てのカテゴリー記事が混在した月別アーカイブを表示させる事は可能でしょうか? 質問ばかりですみません....
退会済みユーザー

退会済みユーザー

2017/03/07 08:20

条件を後出しされても困るのですが…… そういう条件であれば、以下のようにすればいいかと思います。 (1) wp_get_archivesをコピペし、複数の投稿タイプ入力に対応したアーカイブリスト表示関数を新規作成 (2) pre_get_postsにアーカイブのタイプによって投稿タイプを設定するように関数をフック
MOMONUTS

2017/03/08 00:13

miz様 重要な部分が後出しの様な形になってしまい、大変申し訳ございません。 上記の方法ですが、具体的にどの様に記述すれば良いか分からず、未だ全カスタム投稿ページで全記事の月別アーカイブが出てしまっている状態です。 どの様に記述すれば良いかご指導を賜れますでしょうか... 本当に何度も申し訳ございません...m(_ _)m
guest

0

PHP

1// 文字数制限に引っかかったので、回答を分割して記載 2 3 if ( 'monthly' == $r['type'] ) { 4 $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date $order $limit"; 5 $key = md5( $query ); 6 $key = "wp_get_archives:$key:$last_changed"; 7 if ( ! $results = wp_cache_get( $key, 'posts' ) ) { 8 $results = $wpdb->get_results( $query ); 9 wp_cache_set( $key, $results, 'posts' ); 10 } 11 12 if ( $results ) { 13 $after = $r['after']; 14 foreach ( (array) $results as $result ) { 15 $url = get_month_link( $result->year, $result->month ); 16 /* translators: 1: month name, 2: 4-digit year */ 17 $text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $result->month ), $result->year ); 18 if ( $r['show_post_count'] ) { 19 $r['after'] = '&nbsp;(' . $result->posts . ')' . $after; 20 } 21 22 if( !empty($r['post_type']) ) { 23 $apt = implode( ',', $r['post_type'] ); 24 $url = add_query_arg( 'apt', $apt, $url ); 25 } 26 27 $format = (empty($r['format']))?('html'):($r['format']); 28 $before = (empty($r['before']))?(''):($r['before']); 29 $after = (empty($r['after']))?(''):($r['after']); 30 31 $text = wptexturize($text); 32 $url = esc_url($url); 33 34 if ('link' == $format) 35 $link_html = "\t<link rel='archives' title='" . esc_attr( $text ) . "' href='$url' />\n"; 36 elseif ('option' == $format) 37 $link_html = "\t<option value='$url'>$before $text $after</option>\n"; 38 elseif ('html' == $format) 39 $link_html = "\t<li>$before<a href='$url'>$text</a>$after</li>\n"; 40 else // custom 41 $link_html = "\t$before<a href='$url'>$text</a>$after\n"; 42 43 $output .= $link_html; 44 } 45 } 46 } elseif ( 'yearly' == $r['type'] ) { 47 $query = "SELECT YEAR(post_date) AS `year`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date) ORDER BY post_date $order $limit"; 48 $key = md5( $query ); 49 $key = "wp_get_archives:$key:$last_changed"; 50 if ( ! $results = wp_cache_get( $key, 'posts' ) ) { 51 $results = $wpdb->get_results( $query ); 52 wp_cache_set( $key, $results, 'posts' ); 53 } 54 if ( $results ) { 55 $after = $r['after']; 56 foreach ( (array) $results as $result) { 57 $url = get_year_link( $result->year ); 58 $text = sprintf( '%d', $result->year ); 59 if ( $r['show_post_count'] ) { 60 $r['after'] = '&nbsp;(' . $result->posts . ')' . $after; 61 } 62 63 if( !empty($r['post_type']) ) { 64 $apt = implode( ',', $r['post_type'] ); 65 $url = add_query_arg( 'apt', $apt, $url ); 66 } 67 68 $format = (empty($r['format']))?('html'):($r['format']); 69 $before = (empty($r['before']))?(''):($r['before']); 70 $after = (empty($r['after']))?(''):($r['after']); 71 72 $text = wptexturize($text); 73 $url = esc_url($url); 74 75 if ('link' == $format) 76 $link_html = "\t<link rel='archives' title='" . esc_attr( $text ) . "' href='$url' />\n"; 77 elseif ('option' == $format) 78 $link_html = "\t<option value='$url'>$before $text $after</option>\n"; 79 elseif ('html' == $format) 80 $link_html = "\t<li>$before<a href='$url'>$text</a>$after</li>\n"; 81 else // custom 82 $link_html = "\t$before<a href='$url'>$text</a>$after\n"; 83 84 $output .= $link_html; 85 } 86 } 87 } elseif ( 'daily' == $r['type'] ) { 88 $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date $order $limit"; 89 $key = md5( $query ); 90 $key = "wp_get_archives:$key:$last_changed"; 91 if ( ! $results = wp_cache_get( $key, 'posts' ) ) { 92 $results = $wpdb->get_results( $query ); 93 wp_cache_set( $key, $results, 'posts' ); 94 } 95 if ( $results ) { 96 $after = $r['after']; 97 foreach ( (array) $results as $result ) { 98 $url = get_day_link( $result->year, $result->month, $result->dayofmonth ); 99 $date = sprintf( '%1$d-%2$02d-%3$02d 00:00:00', $result->year, $result->month, $result->dayofmonth ); 100 $text = mysql2date( get_option( 'date_format' ), $date ); 101 if ( $r['show_post_count'] ) { 102 $r['after'] = '&nbsp;(' . $result->posts . ')' . $after; 103 } 104 105 if( !empty($r['post_type']) ) { 106 $apt = implode( ',', $r['post_type'] ); 107 $url = add_query_arg( 'apt', $apt, $url ); 108 } 109 110 $format = (empty($r['format']))?('html'):($r['format']); 111 $before = (empty($r['before']))?(''):($r['before']); 112 $after = (empty($r['after']))?(''):($r['after']); 113 114 $text = wptexturize($text); 115 $url = esc_url($url); 116 117 if ('link' == $format) 118 $link_html = "\t<link rel='archives' title='" . esc_attr( $text ) . "' href='$url' />\n"; 119 elseif ('option' == $format) 120 $link_html = "\t<option value='$url'>$before $text $after</option>\n"; 121 elseif ('html' == $format) 122 $link_html = "\t<li>$before<a href='$url'>$text</a>$after</li>\n"; 123 else // custom 124 $link_html = "\t$before<a href='$url'>$text</a>$after\n"; 125 126 $output .= $link_html; 127 } 128 } 129 } elseif ( 'weekly' == $r['type'] ) { 130 $week = _wp_mysql_week( '`post_date`' ); 131 $query = "SELECT DISTINCT $week AS `week`, YEAR( `post_date` ) AS `yr`, DATE_FORMAT( `post_date`, '%Y-%m-%d' ) AS `yyyymmdd`, count( `ID` ) AS `posts` FROM `$wpdb->posts` $join $where GROUP BY $week, YEAR( `post_date` ) ORDER BY `post_date` $order $limit"; 132 $key = md5( $query ); 133 $key = "wp_get_archives:$key:$last_changed"; 134 if ( ! $results = wp_cache_get( $key, 'posts' ) ) { 135 $results = $wpdb->get_results( $query ); 136 wp_cache_set( $key, $results, 'posts' ); 137 } 138 $arc_w_last = ''; 139 if ( $results ) { 140 $after = $r['after']; 141 foreach ( (array) $results as $result ) { 142 if ( $result->week != $arc_w_last ) { 143 $arc_year = $result->yr; 144 $arc_w_last = $result->week; 145 $arc_week = get_weekstartend( $result->yyyymmdd, get_option( 'start_of_week' ) ); 146 $arc_week_start = date_i18n( get_option( 'date_format' ), $arc_week['start'] ); 147 $arc_week_end = date_i18n( get_option( 'date_format' ), $arc_week['end'] ); 148 $url = add_query_arg( array( 'm' => $arc_year, 'w' => $result->week, ), home_url( '/' ) ); 149 $text = $arc_week_start . $archive_week_separator . $arc_week_end; 150 if ( $r['show_post_count'] ) { 151 $r['after'] = '&nbsp;(' . $result->posts . ')' . $after; 152 } 153 154 if( !empty($r['post_type']) ) { 155 $apt = implode( ',', $r['post_type'] ); 156 $url = add_query_arg( 'apt', $apt, $url ); 157 } 158 159 $format = (empty($r['format']))?('html'):($r['format']); 160 $before = (empty($r['before']))?(''):($r['before']); 161 $after = (empty($r['after']))?(''):($r['after']); 162 163 $text = wptexturize($text); 164 $url = esc_url($url); 165 166 if ('link' == $format) 167 $link_html = "\t<link rel='archives' title='" . esc_attr( $text ) . "' href='$url' />\n"; 168 elseif ('option' == $format) 169 $link_html = "\t<option value='$url'>$before $text $after</option>\n"; 170 elseif ('html' == $format) 171 $link_html = "\t<li>$before<a href='$url'>$text</a>$after</li>\n"; 172 else // custom 173 $link_html = "\t$before<a href='$url'>$text</a>$after\n"; 174 175 $output .= $link_html; 176 } 177 } 178 } 179 } elseif ( ( 'postbypost' == $r['type'] ) || ('alpha' == $r['type'] ) ) { 180 $orderby = ( 'alpha' == $r['type'] ) ? 'post_title ASC ' : 'post_date DESC, ID DESC '; 181 $query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit"; 182 $key = md5( $query ); 183 $key = "wp_get_archives:$key:$last_changed"; 184 if ( ! $results = wp_cache_get( $key, 'posts' ) ) { 185 $results = $wpdb->get_results( $query ); 186 wp_cache_set( $key, $results, 'posts' ); 187 } 188 if ( $results ) { 189 foreach ( (array) $results as $result ) { 190 if ( $result->post_date != '0000-00-00 00:00:00' ) { 191 $url = get_permalink( $result ); 192 if ( $result->post_title ) { 193 /** This filter is documented in wp-includes/post-template.php */ 194 $text = strip_tags( apply_filters( 'the_title', $result->post_title, $result->ID ) ); 195 } else { 196 $text = $result->ID; 197 } 198 199 if( !empty($r['post_type']) ) { 200 $apt = implode( ',', $r['post_type'] ); 201 $url = add_query_arg( 'apt', $apt, $url ); 202 } 203 204 $format = (empty($r['format']))?('html'):($r['format']); 205 $before = (empty($r['before']))?(''):($r['before']); 206 $after = (empty($r['after']))?(''):($r['after']); 207 208 $text = wptexturize($text); 209 $url = esc_url($url); 210 211 if ('link' == $format) 212 $link_html = "\t<link rel='archives' title='" . esc_attr( $text ) . "' href='$url' />\n"; 213 elseif ('option' == $format) 214 $link_html = "\t<option value='$url'>$before $text $after</option>\n"; 215 elseif ('html' == $format) 216 $link_html = "\t<li>$before<a href='$url'>$text</a>$after</li>\n"; 217 else // custom 218 $link_html = "\t$before<a href='$url'>$text</a>$after\n"; 219 220 $output .= $link_html; 221 } 222 } 223 } 224 } 225 226 if ( $r['echo'] ) { 227 echo $output; 228 } else { 229 return $output; 230 } 231}

投稿2017/03/09 09:08

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.51%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問