概要
フィルタ cptui_convert_taxonomy_terms
は、WordPressのCustom Post Type UIプラグインと連携して、カスタムタクソノミーのタームを変換・操作する際に使用されます。このフィルタは、特定のタクソノミー用に作成したコードのカスタマイズやデータ操作に役立ちます。具体的には以下のような機能を実装する際によく使われます。
- タクソノミータームのフォーマット変更
- 特定の条件に基づくタームのフィルタリング
- タクソノミーのラベルやスラッグの変更
- タームの追加または削除処理のカスタマイズ
- カスタム条件下でのタームメタデータの操作
- タームの表示順序の変更
構文
add_filter('cptui_convert_taxonomy_terms', 'custom_function', 10, 2);
パラメータ
- $term_list: タームの配列
- $taxonomy: タクソノミーのスラッグ
戻り値
- 変換したタームの配列
使用可能なプラグイン
- Custom Post Type UI: バージョン 1.10.0以上
- WordPress: バージョン 5.0以上
この関数のアクションでの使用可能性
アクション名 | 使用可能 |
---|---|
mu_plugin_loaded | |
registered_post_type | 〇 |
plugins_loaded | 〇 |
wp_roles_init | |
setup_theme | |
after_setup_theme | |
set_current_user | |
init | 〇 |
register_sidebar | |
wp_loaded | |
send_headers | |
parse_query | |
pre_get_posts | |
wp | |
template_redirect | |
get_header | |
wp_head |
サンプルコード1
add_filter('cptui_convert_taxonomy_terms', 'change_taxonomy_terms', 10, 2);
function change_taxonomy_terms($term_list, $taxonomy) {
if ($taxonomy === 'my_custom_taxonomy') {
foreach ($term_list as $key => $term) {
$term_list[$key] = strtoupper($term); // ターム名を大文字に変換
}
}
return $term_list;
}
このサンプルコードは、カスタムタクソノミーmy_custom_taxonomy
のターム名をすべて大文字に変換する機能を実装しています。
サンプルコード2
add_filter('cptui_convert_taxonomy_terms', 'filter_specific_terms', 10, 2);
function filter_specific_terms($term_list, $taxonomy) {
if ($taxonomy === 'category') {
return array_filter($term_list, function($term) {
return strpos($term, 'specific') !== false; // 'specific' を含むタームのみ返す
});
}
return $term_list;
}
このサンプルコードは、カテゴリーのタームから特定のキーワードspecific
が含まれるタームだけをフィルタリングして返します。
サンプルコード3
add_filter('cptui_convert_taxonomy_terms', 'change_term_slug', 10, 2);
function change_term_slug($term_list, $taxonomy) {
if ($taxonomy === 'tags') {
foreach ($term_list as &$term) {
$term = str_replace(' ', '-', $term); // スペースをハイフンに置き換え
}
}
return $term_list;
}
このサンプルコードでは、tags
タクソノミーのターム名内のスペースをハイフンに置き換える処理を実装しています。
サンプルコード4
add_filter('cptui_convert_taxonomy_terms', 'custom_taxonomy_meta', 10, 2);
function custom_taxonomy_meta($term_list, $taxonomy) {
if ($taxonomy === 'custom_meta') {
// 特定のタームに対してメタデータを追加
foreach ($term_list as &$term) {
$term .= ' (Custom Meta)'; // タームの後ろにカスタムメタを追加
}
}
return $term_list;
}
このサンプルコードは、カスタムタクソノミーcustom_meta
のタームに、カスタムメタデータを追加する処理を示しています。
サンプルコード5
add_filter('cptui_convert_taxonomy_terms', 'sort_taxonomy_terms', 10, 2);
function sort_taxonomy_terms($term_list, $taxonomy) {
if ($taxonomy === 'portfolio') {
sort($term_list); // タームリストをアルファベット順にソート
}
return $term_list;
}
このサンプルコードは、タクソノミーportfolio
に関連するタームリストをアルファベット順にソートして返す機能を実装しています。