Use this-
function custom_tag_archive_query( $query ) {
if ( is_tag() && $query->is_main_query() && !is_admin() ) {
$tag = get_queried_object();
$tag_name = $tag->name;
$similar_names = get_term_meta( $tag->term_id, 'similar_names', true );
$similar_names_array = explode( ',', $similar_names );
$similar_names_array = array_map( 'trim', $similar_names_array );
$similar_names_array[] = $tag_name; // include the tag name itself in the search terms
$meta_query = array(
'relation' => 'OR',
);
foreach ( $similar_names_array as $name ) {
$meta_query[] = array(
'key' => 'post_title',
'value' => $name,
'compare' => 'LIKE'
);
}
$query->set( 'meta_query', $meta_query );
}
}
add_action( 'pre_get_posts', 'custom_tag_archive_query' );
Ensure that the custom field similar_names
is set up properly for your tags in the term meta. This code will modify the main query on tag archive pages to include posts whose titles match any word from the similar_names
field or the tag name itself.