Obtendo o valor do relacionamento errado em $ args em wp_Query?

4

Eu estou tentando buscar dados do banco de dados para o meu plugin personalizado e tipo de postagem. meus argumentos de consulta devem ser como:

$args = array(
    'post_type'  => 'products',
    'post_status'=> 'publish',
    'meta_query' => array(
       'relation' => 'OR',
      array( 'key'=>'product_commercial',
       'value'=>'on',
       'compare'=>'='  
    ), 
      array( 'key'=>'product_exterior',
       'value'=>'on',
       'compare'=>'='  
    )
)
);
$search_query = new WP_Query( $args );

Mas , estou tentando adicionar valores de meta-chave dinamicamente como:

$inner_arrays=array();
$count = 0;
foreach($values as $value){
if($value){

        $inner_arrays[$count]['key'] .= $value;
        $inner_arrays[$count]['value'] .= 'on';
        $inner_arrays[$count]['compare'] .= '=';
        echo $count++;
}
}
print_r($inner_arrays);
    //$array =  $array[0] ; 
    $args = array(
    'post_type'  => 'products',
    'post_status'=> 'publish',
    'meta_query' => array(
       'relation' => 'OR',
    $inner_arrays
)
);
//values are some random values (say fetched from db).

Agora, quando imprimo a consulta usando

echo "<pre>Last SQL-Query: {$search_query->request}".'<br/>';

exibe

    Last SQL-Query: SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )  INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id )  INNER JOIN wp_postmeta AS mt2 ON ( wp_posts.ID = mt2.post_id ) WHERE 1=1  AND ( 
  ( 
    ( wp_postmeta.meta_key = 'product_commercial' AND CAST(wp_postmeta.meta_value AS CHAR) = 'on' ) 
    **AND** 
    ( mt1.meta_key = 'product_framed' AND CAST(mt1.meta_value AS CHAR) = 'on' ) 
    **AND** 
    ( mt2.meta_key = 'product_horizontal' AND CAST(mt2.meta_value AS CHAR) = 'on' )
  )
) AND wp_posts.post_type = 'products' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10

PROBLEM : estou usando relation => OR , mas recebendo AND na consulta SQL. O que estou fazendo de errado?

    
por user101703 26.08.2016 / 14:23

1 resposta

0

add: $inner_arrays['relation'] = 'OR'; e, em seguida, a consulta será como:

$args = array(
    'post_type'  => 'products',
    'post_status'=> 'publish',
    'meta_query' => $inner_arrays
);

isso funciona ....:)

    
por Esar-ul-haq Qasmi 26.08.2016 / 15:41