You can’t use multiple arguments of the same name. You’ve passed 3 name__like
arguments. This isn’t going to work the way you expect.
Arguments that support multiple possible values will accept an Array, but name__like
is not one of those arguments. name__like
can only be a String.
The value passed to name like is passed into the SQL like this in WP_Term_Query::get_terms()
:
$this->sql_clauses['where']['name__like'] = $wpdb->prepare( "t.name LIKE %s", '%' . $wpdb->esc_like( $args['name__like'] ) . '%' );
So the value passed there is put directly into t.name LIKE %(here)%
. But it’s also excaped, so you can’t use something like this (I tried):
'name__like' => 'worker% OR t.name LIKE %group'
The closest thing WP_Term_Query
supports is multiple names:
'name' => ['worker', 'student', 'group']
But that will only match terms that are exactly worker, student or group.
To do what you’re after will either require doing 3 separate calls to get_tags()
or get_terms()
with different values for name__like
, or doing the query manually with $wpdb
:
global $wpdb;
$results = $wpdb->get_results(
"SELECT
t.*, tt.*
FROM
wp_terms AS t
INNER JOIN
wp_term_taxonomy AS tt ON
t.term_id = tt.term_id
WHERE
tt.taxonomy = 'post_tag' AND
(
t.name LIKE '%worker%' OR
t.name LIKE '%group%' OR
t.name LIKE '%student%'
)
ORDER BY
t.name ASC"
);
if ( ! empty( $results ) ) {
$tags = array_map( 'get_term', $results ); // Turn each result into a proper WP_Term object.
foreach ( $tags as $tag ) {
echo $tag->name;
}
}
Note that if those particular words are dynamic, you’ll need to prepare the query with $wpdb->prepare()
.