2

I have an options page in my wordpress theme, it's to select a number of categories from a custom taxonomy.

$terms_obj = get_option('shop_features')

This returns an array with $key being the category-name and $value being either 1 or empty depending if the category was checked.

I need to grab a list of the checked categories to use in an array in another function:

add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;

if ( ! is_admin() && is_shop() ) {

    $q->set( 'tax_query', array(array(
        'taxonomy' => 'product_cat',
        'field' => 'slug',
        'terms' => array( 'knives' ), // Don't display products in the knives category on the shop page
        'operator' => 'NOT IN'
    )));

}

remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

}

Where I need to insert my category-names contained in a variable $terms to replace the 'terms' => array('knives') with 'terms' => array ( $terms )

Except it doesn't work by just doing that!

Here is how I've tried to accomplish that:

function custom_pre_get_posts_query( $q ) {

if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;

if ( ! is_admin() && is_shop() ) {

    $terms_obj = of_get_option( 'eco_shop_features', $default );
    foreach ( $terms_obj as $slug => $checked ) { //$key ==> value
        if ( $checked > 0 )  
            $terms .= '\'' . $slug . '\', ';
    }
    $terms = rtrim( $terms, ', ' );

    $q->set( 'tax_query', array(array(
        'taxonomy' => 'product_cat',
        'field' => 'slug',
        'terms' => array( "$terms" ), // Don't display products in the knives category on the shop page
        'operator' => 'NOT IN'
    )));

}

remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

}

I'm stuck of how to insert just the list of category names in to the terms field so it works as an array.

3 Answers 3

3

The right way seems to be commented out by you, below should work

foreach ( $terms_obj as $slug => $checked ) { //$key ==> value
      if ( $checked > 0 )  
         $terms[] = $slug;
}

$q->set( 'tax_query', array(array(
         'taxonomy' => 'product_cat',
         'field' => 'slug',
         'terms' => $terms,
         'operator' => 'NOT IN'
)));
Sign up to request clarification or add additional context in comments.

3 Comments

omg thank you so much - i was pretty sure I tried that as well (hence why I had that line in there but commented out. I guess I didn't try placing it correctly as 'terms' => $terms PS minions are awesome!
Glad to help you. And yeah Minions brings smile on our face :)
Marked as answer! Thank you again!
1

To get your array of category names you can do :

    $terms_obj = get_option('shop_features');
    $category_name_array = array_keys($terms_obj, 1);

The array_keys($terms_obj, 1) function will extract in an array all the keys of the $terms_obj for witch the value match 1.

More info : http://php.net/manual/en/function.array-keys.php

Comments

0

Just prepare an array in the loop

$terms[] = $slug

And assign to term

 'terms' => $terms,

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.