検索キーワードを入力:

wp_get_archivesで、特定カテゴリ(term)を除外もしくは含まれるリスト作成

というかむしろ、カテゴリ別のwp_get_archivesの作成ってタイトルでもいいんだけどね。

まぁいいや。需要無いし。
プラグインに頼り切るのはいかん、ということで、簡単なコードで行けることに気付いた昨今。
いかがお過ごしでしょうか。

早速コード

[php]
add_filter( ‘getarchives_where’, ‘customarchives_where’ );
add_filter( ‘getarchives_join’, ‘customarchives_join’ );

function customarchives_join( $x ) {
$tax_query = array ( array( ‘taxonomy’ => ‘category’, ‘terms’ => array ( 4 ), ‘operator’ => ‘IN’ ) );
$clauses = get_tax_sql ( $tax_query, $wpdb->posts, ‘ID’ );

return $x . $clauses[‘join’];
}

function customarchives_where( $x ) {
$tax_query = array ( array( ‘taxonomy’ => ‘category’, ‘terms’ => array ( 4 ), ‘operator’ => ‘IN’ ) );
$clauses = get_tax_sql ( $tax_query, $wpdb->posts, ‘ID’ );

return $x . $clauses[‘where’];
}
[/php]

含めるときはoperatorをINで、除外するときはoperatorをNOT INに変更しましょう。

現在、作ってるサイトで、ブログはカスタムポストタイプ『blog』で。
しかし、別のポストタイプの、あるカテゴリ以外は月別アーカイブに含めたいとの要望が。

ということで、月別アーカイブのリストを作成しましょう。

カスタムポストタイプの月別アーカイブの作成は簡単に出来るけど、そこに別のポストタイプの特定カテゴリ以外も含ませちゃう。
例えば、post_typeがblogの月別アーカイブにpost_typeがpostのitemカテゴリ以外を含ませちゃう。
めんどくさいけど、これはwp_get_archives使うんじゃなくて、別に作っちゃう。

[php]
function wp_custom_archive($args = ”) {
global $wpdb, $wp_locale;
$defaults = array(
‘limit’ => ”,
‘format’ => ‘html’, ‘before’ => ”,
‘after’ => ”, ‘show_post_count’ => false,
‘echo’ => 1
);
$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );
if ( ” != $limit ) {
$limit = absint($limit);
$limit = ‘ LIMIT ‘.$limit;
}

$item_cat = get_category_by_slug(‘item’);
$tax_query = array ( array( ‘taxonomy’ => ‘category’, ‘terms’ => array ( $item_cat->term_id ), ‘operator’ => ‘NOT IN’ ) );
$clauses = get_tax_sql ( $tax_query, $wpdb->posts, ‘ID’ );
$where = "WHERE (post_type = ‘blog’ AND post_status = ‘publish’) or (post_type = ‘post’ AND post_status = ‘publish’ {$clauses[‘where’]})";
$join = $clauses[‘join’];
$output = ”;
$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 ASC $limit";
$key = md5($query);
$cache = wp_cache_get( ‘wp_custom_archive’ , ‘general’);
if ( !isset( $cache[ $key ] ) ) {
$arcresults = $wpdb->get_results($query);
$cache[ $key ] = $arcresults;
wp_cache_set( ‘wp_custom_archive’, $cache, ‘general’ );
} else {
$arcresults = $cache[ $key ];
}
if ( $arcresults ) {
$afterafter = $after;
$i = 0;
foreach ( (array) $arcresults as $arcresult ) {
$url = get_month_link( $arcresult->year, $arcresult->month );
$text = $text_m = $arcresult->month;
$year_text = sprintf(‘<span>%d</span>’, $arcresult->year);
if ( $show_post_count )
$after = ‘&nbsp;(‘.$arcresult->posts.’)’ . $afterafter;
if($arcresult->year != $temp_year) $i++;
$html[$i][‘year’] .= ( $arcresult->year != $temp_year ) ? $year_text : ”;
$html[$i][‘month’] .= get_archives_link($url, $text_m, $format, $before, $after);
$temp_year = $arcresult->year;
}
foreach ( $html as $htm ) {
$output = $htm[‘year’]."\n".'<ul>’."\n".$htm[‘month’].'</ul>’."\n".$output."\n";
}
$output = str_replace("date/","blog/date/",$output);
}
if ( $echo )
echo $output;
else
return $output;
}
[/php]

後半は適当なので、あまり期待しない方がイイです。

以上です。

About Little

WordPressをいじくり倒して早10年。一人ぼっちでひたすらソースとにらめっこ厨。 有り難いことに、Welcart/WP e-commerce/WooCommerce/EC-Cube等で多数ECサイトを制作させていただいたけど、ふと気付いた、ちゃんと売れるのか。 効果的に売れるようにするためにはどうしたらいいのか。ということでお勉強を兼ねてECサイトも運営中。
2012年5月23日

Related Posts

0 comments found

Comments for: wp_get_archivesで、特定カテゴリ(term)を除外もしくは含まれるリスト作成

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です