get_the_tags()
returns an array of WP_Term
objects. Even if there’s only one tag, it returns an array with one item.
You can use wp_list_pluck()
to check the WP_Term
objects in the array, though, and so your code can be re-written:
function web_alert($content) {
global $wpdb; // A side note: this doesn't seem to be necessary.
$alert = get_blog_post( 2, 1 );
if (
$alert->post_status == 'publish' &&
in_array( 'my-tag', wp_list_pluck( get_the_tags( $alert ), 'name' ) )
) {
$content="<div class="alert">" . $alert->post_content . '</div>';
return $content;
}
}
add_shortcode('webalert', 'web_alert');
wp_list_pluck( get_the_tags( $alert ), 'name' )
should return an array of the post’s tag names, which you can then check for your desired tag with in_array()
.