Skip to content

Instantly share code, notes, and snippets.

@gatespace
Last active April 25, 2018 10:47
Show Gist options
  • Save gatespace/8a1e2393d3577f43657bd77a62755e29 to your computer and use it in GitHub Desktop.
Save gatespace/8a1e2393d3577f43657bd77a62755e29 to your computer and use it in GitHub Desktop.
[WordPress] ターム(カテゴリー・タグ)の記事が指定件数以下なら一覧などに出力しない ref: https://qiita.com/gatespace/items/efc3792e00e55b50b53c
<?php
// get_terms でタグに所属する投稿が指定した件数より少なかったら表示しない
add_filter( 'get_terms', 'my_get_terms', 10, 4 );
function my_get_terms( $terms, $taxonomies, $args, $term_query ) {
if ( is_admin() ) { // 管理画面だったら何もしない
return $terms;
}
if ( $taxonomies[0] != 'post_tag' ) { // 条件分岐はよしなに
return $terms;
}
$new_terms = array();
foreach ( $terms as $term ) {
if ( $term->count >= 6 ) {
$new_terms[] = $term;
}
}
return $new_terms;
}
<?php
// get_the_terms でタグに所属する投稿が指定した件数より少なかったら表示しない
add_filter( 'get_the_terms', 'my_get_the_terms', 10, 3 );
function my_get_the_terms( $terms, $post_id, $taxonomy ) {
if ( is_admin() ) { // 管理画面だったら何もしない
return $terms;
}
if ( $taxonomy != 'post_tag') { // 条件分岐はよしなに
return $terms;
}
$new_terms = array();
foreach ( $terms as $term ) {
if ( $term->count >= 6 ) {
$new_terms[] = $term;
}
}
return $new_terms;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment