Tax_query terms ID’s using variable

It looks like you are making an array with a single string inside.

Check if making $tax into an array before passing it will work:

$tax = array( 19, 18, 214, 226, 20 );

$query_args = array (
    'post_type' => 'works',
    'tax_query' => array(
        array(
            'taxonomy'  => 'materials',
            'field'     => 'term_id',
            'terms'     => $tax,
        )
    ),
);

If you need to make an array from a formatted string, you can use the explode PHP function that takes a delimiter and a string, and returns an array, like so:

$tax_string = '19,18,214,226,20';
$tax_array = explode( ',', $tax_string );

Hope that works!

Leave a Comment