My WordPress theme has an option page, it has to select several categories from a custom classification.
[ ] $ terms_obj = get_option ('shop_features')
This gives an array with the $ key is the category-name and $ value is either 1 or Depending on empty if category
add_action ('pre_get_posts', 'custom_pre_get_posts_query'); I need to get a list of categories checked to use in an array in another function. Function custom_pre_get_posts_query ($ q) {if (! $ Q- & gt; is_main_query ()) returns; If (! $ Q- & gt; is_post_type_archive ()) returns; If (! Is_admin () and is_shop ()) {$ q- & gt; Set ('tax_corea', array (array '' categorization ''> 'product_kat', 'field' => slug '' rule '= & gt; array (' knife '), // shop page' Do not display products in the knife category on operator '= & gt; No;)}} Remove_action (' pre_get_posts', 'custom_pre_get_posts_query');}
Names are required to be contained in a convertible $ word where the 'terms' => array ('knife')
with 'terms' = & gt; array ($ word) )
Except that this is not the only way to do it
Here I have tried to accomplish this:
function custom_pre_get_posts_query ($ q) {if (! $ Q- & gt; is_main_query ( ) Returns; if (! $ Q-> is_post_type_archive ()) returns; if (! Is_admin () and is_shop ()) {$ terms_obj = of_get_option ('eco_shop_features', $ default); Foreign currency ($ Word_best $$ slug = & gt; $ checked) {// $ key ==> If value ($ checked & gt; 0) $ word = '\'. $ Slug '\', ';} $ Word = rtrim ($ word,', '); $ Q- & gt; Set ('tax_query', array (array ('taxonomy' = & gt; 'product_cat', 'field' => 'slug', 'rule' = & gt; array ("$ terms"), / store Page 'operator' = & gt; Do not display the product in knife category on 'no')); } Remove_action ('pre_get_posts', 'custom_pre_get_posts_query'); }
I am stuck in the field field to include a list of only category names, so it acts as an array.
You are rightly commented, should work under
foreach ($ terms_obj as $ slug = & gt; $ check) {// $ key == & gt; Value if ($ checked & gt; 0) $ word [] = $ slug; } $ Q- & gt; Set ('tax_query', array (array ('taxonomy' = & gt; 'product_cat', 'field' = & gt; slug ',' rule '= & gt; $ terms,' operator '= & gt; Not inside' )));
Comments
Post a Comment