匹配WordPress中的任何标签


Match any tags in WordPress

我有一个简单的函数,如果帖子有任何匹配的术语,它会为帖子分配一个类别(使用save_post)。问题是,虽然它只适用于一个术语,但尝试匹配多个术语是行不通的。这项工作:

function categorize_from_tags( $post_id ) {
    $cat_boosted = 'cat-boosted';
    $terms = 'twin-turbo';
    if( has_term( $terms, 'post_tag' ) ) {
        wp_set_object_terms( $post_id, $cat_boosted, 'category', true );
    }
}
add_action( 'save_post', 'categorize_from_tags', 120, 1 );

当我添加多个术语时,它不起作用:

function categorize_from_tags( $post_id ) {
    $cat_boosted = 'cat-boosted';
    $terms = 'twin-turbo,ugr'; // Adding more than one term
    if( has_term( $terms, 'post_tag' ) ) {
        wp_set_object_terms( $post_id, $cat_boosted, 'category', true );
    }
}
add_action( 'save_post', 'categorize_from_tags', 120, 1 );

您发送的类别名称为"twin-turbo,ugr"。

你想发送这样的数组:

$terms = ['twin-turbo','ugr'];

请确保检查了要使用的WP Global函数的参数。

has_term接受要匹配的字符串或字符串数组。

相关文章: