I having an issue getting a variable to work for one of my arguments in a query. I am using a custom post type and category name as terms to determine which categories are displayed.
When I hardcode the values into the terms it works fine, but when I use a variable it doesn't seem to work.
This code works:
$args = array(
'post_type' => 'sparknz',
'tax_query' => array(
array(
'taxonomy' => 'sparknz_gardens',
'field' => 'name',
'terms' => array ( 'The A Team', 'The B Team', 'The C Team' ),
)
)
);
But this does not (notice variable in terms):
$my_term_names = "'The A Team','The B Team','The C Team'";
$args = array(
'post_type' => 'sparknz',
'tax_query' => array(
array(
'taxonomy' => 'sparknz_gardens',
'field' => 'name',
'terms' => array( $my_term_names ),
)
)
);
I need the terms to be a variable. Any Ideas?
Just an update as to why I am using a string as a variable. I am using array_intersect
to pick out similarities in two arrays:
$my_user_array = array( "c" => $user_array );
$my_cat_array = array( "d" => $category_array );
$myresult = array_intersect( $my_user_array, $my_cat_array );
$my_term_names = implode( ",", $myresult );
echo $$my_term_names;
Not sure if there is another way to do this?
$my_term_names
is a single string, your original array is has multiple indices containing strings. Even though your string has commas in it PHP will not separate them into values. Why name just make$my_term_names
into an array with 3 string values?