Wordpress get_terms not allowing array variable

110
December 24, 2019, at 02:50 AM

Okay, this is one of those weird ones, trying to pass a generated array to get_terms, tried it directly with $compiledList, indirectly with implode() and re-building the array into a string, no luck, is there something simple I'm missing?

$thearray = [];
$thelister = [];
$compiledList = [];
$args = array(
    'meta_query'  => array(
                    '_stock_status' => 'instock'
        )
);
foreach( wc_get_products(array($args, 'limit' => -1)) as $product ){
    foreach( $product->get_attributes() as $attr_name => $attr ){
        $terms = "pa_" . strtolower(wc_attribute_label( $attr_name )) .",";
        array_push($thelister, $terms);
        // or get_taxonomy( $attr_name )->labels->singular_name;
    }
}
        $thenames = array_unique($thelister);
        foreach($thenames as $tname)
        {
            array_push($compiledList, $tname);
        }
$compiledList = implode('', $compiledList);
$compiledList = str_replace('pa_', '"pa_', $compiledList);
$compiledList = str_replace(',', '",', $compiledList);
$compiledList = substr($compiledList, 0, -1);
$compiledList = strval($compiledList);
echo $compiledList;

print("<pre>".print_r($compiledList,true)."</pre>");
$theterms = get_terms([$compiledList]);
foreach ( $theterms as $term ) {
$categories =  $term->name;
$categories = str_replace('&amp;', 'and', $categories);
array_push($thearray, $categories);
}
Answer 1

It was really simple after all

replaced

$compiledList = implode('', $compiledList);
$compiledList = str_replace('pa_', '"pa_', $compiledList);
$compiledList = str_replace(',', '",', $compiledList);
$compiledList = substr($compiledList, 0, -1);
$compiledList = strval($compiledList);
echo $compiledList;

with

$compiledList = implode("", $compiledList);
$compiledList = substr($compiledList, 0, -1);
$compiledList = explode(',', $compiledList);
READ ALSO
How to store all elements in database? [closed]

How to store all elements in database? [closed]

Want to improve this question? Update the question so it's on-topic for Stack Overflow

121
Getting a symbol from database is outputting � [duplicate]

Getting a symbol from database is outputting � [duplicate]

This is a very strange problem I have never experienced before

93
array_search for multidimensional array with two attributes [duplicate]

array_search for multidimensional array with two attributes [duplicate]

Assume we have the following 2-dimensional array:

76
Url_rewrite and php: same structure different url requests

Url_rewrite and php: same structure different url requests

How can I rewrite urls with the same structure to different variables? For example:

94