All in One SEO(AIOSEO)のフィルターで特定のカスタムフィールドの値で該当する記事を除外する方法を備忘録として残します。
背景
特定の記事を対象のユーザーのみに公開するため、カスタムフィールドの値を参照してnoindexやサイトマップを非掲載にする必要があり、本記事の対応を行いました。
公式ドキュメントにあるaioseo_sitemap_exclude_postsフィルターフックを使用して実装します。
add_filter( 'aioseo_sitemap_exclude_posts', 'my_aioseo_exclude_secret_plans', 10, 2 );
function my_aioseo_exclude_secret_plans( $ids, $type ) {
if ( 'general' !== $type ) {
return $ids;
}
$secret_posts = get_posts( [
'post_type' => '投稿タイプ',
'post_status' => 'publish',
'fields' => 'ids',
'nopaging' => true,
'meta_query' => [
[
'key' => 'status',
'value' => 'secret',
'compare' => '='
]
]
] );
if ( ! empty( $secret_posts ) ) {
$ids = array_unique( array_merge( (array) $ids, $secret_posts ) );
}
return $ids;
}
meta_queryに非掲載にしたい対象のカスタムフィールドを記載し、post_typeに対象の投稿タイプを記載。
以上です。