2015-12-01 30 views
0

我使用簡單的形式發送值的搜索內部查詢wordpress,但在某些情況下,從這個簡單的形式沒有所有的值需要搜索,在某些情況下,一個值或3值等,我用這個對於搜索,該搜索基於在屬性WordPress的和使用條件數組

$args = array 
( 
'post_type'    => 'product', 
'post_status'   => 'publish', 
'ignore_sticky_posts' => 1, 
'orderby'    => $orderby, 
'order'     => $order, 
'posts_per_page'  => $per_page, 



'meta_query'=> array 
(
array 
(
'key'   => '_visibility', 
'value'   => array('catalog', 'visible'), 
'compare'  => 'IN' 
) 
), 


'tax_query' => array 
(
/// 
array 
(
'taxonomy'  => 'pa_' . $attribute, 
'terms'   => explode(",",$values), 
'field'   => 'slug', 
'operator'  => 'IN' 
), 

array 
(
'taxonomy'  => 'pa_colors', 
'terms'   => explode(",","Verdes"), 
'field'   => 'slug', 
'operator'  => 'IN' 
), 
/// 
) 


); 

it'si需要在更多1個值的許多時刻搜索,對於每個新的值i需要把其它陣列內部功能,例如,該情況下:

array 
    (
    'taxonomy'  => 'pa_colors', 
    'terms'  => explode(",","Green"), 
    'field'   => 'slug', 
    'operator'   => 'IN' 
    ), 

    ) 

通過這個我的問題是,如果有可能把條件(如,等等)對於每個陣列,我知道沒有權利這樣的語法我現在軟件寫,但瞭解我的想法:

if(isset($_POST['colors'])) 
{ 
    array 
     (
     'taxonomy'  => 'pa_colors', 
     'terms'  => explode(",","Green"), 
     'field'   => 'slug', 
     'operator'   => 'IN' 
     ), 

     ) 
} 

That's我的問題,我怎麼能做到這一點,thank's幫助我,並把

回答

0

如果我理解正確的話,那麼你可以定義$args

$args = array ( 
    'post_type'    => 'product', 
    'post_status'   => 'publish', 
    'ignore_sticky_posts' => 1, 
    'orderby'    => $orderby, 
    'order'     => $order, 
    'posts_per_page'  => $per_page, 
); 

然後有條件地添加任何其他參數爲$args數組:

if(isset($_POST['colors'])){ 
    $terms = explode(",",$_POST['colors']); 
    $args['tax_query''] = array( 
     array(
     'taxonomy'  => 'pa_colors', 
     'terms'  => $terms, 
     'field'   => 'slug', 
     'operator'   => 'IN' 
    ), 
    ); 
}