以下代码表示为一个连续字符串:
$my_term_names = "\'The A Team\',\'The B Team\',\'The C Team\'";
您希望这些术语是一个数组:
$my_term_names = array( \'The A Team\', \'The B Team\', \'The C Team\' );
现在您需要删除
array()
从您的术语查询。
$args = array(
\'post_type\' => \'sparknz\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'sparknz_gardens\', // taxonomy name
\'field\' => \'name\', // term_id, slug or name
\'terms\' => $my_term_names, // Terms array (defined above)
)
)
);
Edit. 10/19/2016
我知道您想使用
array_intersect();
然而,一旦找到了相似之处,就需要停止尝试将所有内容恢复为字符串格式。
array_intersect();
获取多个数组,查找相似性,并返回一个新数组,其中包含指定的多个数组之间的相似值。
Example #1 array_intersect()
example
$array1 = array( "a" => "green", "red", "blue" );
$array2 = array( "b" => "green", "yellow", "red" );
$result = array_intersect( $array1, $array2 );
print_r( $result );
The above example will output:
Array
(
[a] => green
[0] => red
)
如果您计划从
tax_query
, 这个
terms
的参数
tax_query
需要加入
array format . 非字符串格式。
Here\'s an updated example for your situation. I\'ve used simple variable names to get the point across:
$terms_array_1 = array( \'The B Team\', \'The D Team\', \'The A Team\' );
$terms_array_2 = array( \'The A Team\', \'The B Team\', \'The C Team\' );
$similar_terms = array_intersect( $terms_array_1, $terms_array_2 );
print_r( $similar_terms );
$args = array(
\'post_type\' => \'sparknz\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'sparknz_gardens\', // taxonomy name
\'field\' => \'name\', // term_id, slug or name
\'terms\' => $similar_terms, // Term Names array (similarities between array1 and array2)
)
)
);