Reputation: 5994
I have a simple function that assigns a category to a post (using save_post) IF the post has ANY matching terms. The problem is that while it works with ONE term, trying to match multiple terms is not working. This works:
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 );
When I add multiple terms, it does NOT work:
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 );
Upvotes: 0
Views: 42
Reputation: 117
You are sending in a category name of 'twin-turbo,ugr'.
You want to send in an array like so:
$terms = ['twin-turbo','ugr'];
Make sure you check the parameters of the WP Global function you are trying to use.
has_term accepts a string or an array of strings to match against.
Upvotes: 3