1

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?

  • 1
    $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? – Howdy_McGee Oct 17 '16 at 22:13
  • Hi Howdy_McGee, I am using a string because I am using 'array_intersect' and then listing results as a string. – aido14 Oct 17 '16 at 22:18
  • Sorry Howdy Mcgee I spoke too soon. I am still stuck with the same issue because of my 'array_intersect' statement giving back a string. – aido14 Oct 17 '16 at 23:35
  • @aido14 If your question has been answered, could you please accept the solution. Thanks. – Michael Ecklund Jan 4 '17 at 20:14
2

PHP interprets those arrays differently. In the first example you're asking WP_Query to get posts
by 3 term slugs and in the second example you're assigning the array a value of 1 string. If we were to print the two arrays it would look like this:

Your Example 1:

'terms' => array(
    [0] => 'The A Team',
    [1] => 'The B Team',
    [2] => 'The C Team'
)

Your Example 2:

'terms' => array(
    [0] => '\'The A Team','The B Team','The C Team\''
)

PHP thinks that you're assigning it a single string, even if there's commas in the string itself it is an absolute string. What you'll need to do is make the variable itself an array.

$my_term_names = array(
    'The A Team',
    'The B Team',
    'The C Team'
)

'terms' => $my_term_names
  • Thank you very much Howdy, work perfectly. Made perfect sense with your explanation too. Awesome!! – aido14 Oct 17 '16 at 22:58
  • I did get an error because of missing colon ; at end of array, if you want to edit – aido14 Oct 17 '16 at 23:01
  • Can I use str_split() ? – aido14 Oct 17 '16 at 23:44
  • @aido14 - explode() may be a better solution to whatever it is you're trying to do. – Howdy_McGee Oct 17 '16 at 23:47
  • Thanks Howdy, but explode needs to be a string to start off with. Looking at my code can you see a way I could make this work and give an example? – aido14 Oct 18 '16 at 0:38
2

The following code is represented as one continuous string:

$my_term_names = "'The A Team','The B Team','The C Team'";

You want these terms to be an array:

$my_term_names = array( 'The A Team', 'The B Team', 'The C Team' );

Now you need to remove the array() from your terms query.

$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

I realize you want to find the similarities between multiple arrays using array_intersect(); however, you need to stop trying to bring everything back into string format once you have your similarities located. array_intersect(); takes multiple arrays, finds the similarities, and returns a new array, containing the similar values between the multiple arrays specified.

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
)

If you plan on retrieving multiple terms from your tax_query, the terms argument of the tax_query needs to be in array format . Not string 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)
        )
    )
);
  • Hi Michael, I spoke too soon. I am using an array_intersect which gives returns a string. Is there any way you know where I could return something that I can use in my 'terms'? – aido14 Oct 17 '16 at 23:38
  • array_intersect() returns an array containing all the values of array1 that are present in all the arguments. Note that keys are preserved. – Michael Ecklund Oct 19 '16 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.