带有OR的Wordpress WP_Query where子句?

时间:2013-10-28 09:00:17

标签: php wordpress

你好朋友,这是我对WP_Query的争论。

$args = array('post_type' => 'job_listing');
$args['meta_query']=array(
   'relation' => 'OR',
    array(
        'key' => 'app_trailer-type',
        'value' => $job_tailor,
        'compare' => 'LIKE'
    ),
    array(
        'key' => 'app_pay-type',
        'value' => $app_pay_type,
        'compare' => 'LIKE'
    ),
    array(
        'key' => 'geo_address',
        'value' => $geo_address,
        'compare' => 'LIKE'
    ),
    array(
        'key' => 'geo_country',
        'value' => $geo_country,
        'compare' => 'LIKE'
    ),
    array(
        'key' => 'geo_short_address',
        'value' => $geo_short_address,
        'compare' => 'LIKE'
    ),
);

这是返回查询,如。

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)
INNER JOIN wp_postmeta AS mt3 ON (wp_posts.ID = mt3.post_id)
INNER JOIN wp_postmeta AS mt4 ON (wp_posts.ID = mt4.post_id) WHERE 1=1  AND wp_posts.post_type = 'job_listing' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'expired' OR wp_posts.post_status = 'tr_pending' OR wp_posts.post_status = 'tr_failed' OR wp_posts.post_status = 'tr_completed' OR wp_posts.post_status = 'tr_activated' OR wp_posts.post_status = 'private') AND ( (wp_postmeta.meta_key = 'app_trailer-type' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '%Flatbed%')
AND  (mt1.meta_key = 'app_pay-type' AND CAST(mt1.meta_value AS CHAR) LIKE '%Per Week%')
AND  (mt2.meta_key = 'geo_address' AND CAST(mt2.meta_value AS CHAR) LIKE '%Davenport%')
AND  (mt3.meta_key = 'geo_country' AND CAST(mt3.meta_value AS CHAR) LIKE '%United States%')
AND  (mt4.meta_key = 'geo_short_address' AND CAST(mt4.meta_value AS CHAR) LIKE '%Davenport%') ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10

我需要查询查询,如。

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)
INNER JOIN wp_postmeta AS mt3 ON (wp_posts.ID = mt3.post_id)
INNER JOIN wp_postmeta AS mt4 ON (wp_posts.ID = mt4.post_id) WHERE 1=1  AND wp_posts.post_type = 'job_listing' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'expired' OR wp_posts.post_status = 'tr_pending' OR wp_posts.post_status = 'tr_failed' OR wp_posts.post_status = 'tr_completed' OR wp_posts.post_status = 'tr_activated' OR wp_posts.post_status = 'private') AND ( (wp_postmeta.meta_key = 'app_trailer-type' OR CAST(wp_postmeta.meta_value AS CHAR) LIKE '%Flatbed%')
OR  (mt1.meta_key = 'app_pay-type' AND CAST(mt1.meta_value AS CHAR) LIKE '%Per Week%')
OR  (mt2.meta_key = 'geo_address' AND CAST(mt2.meta_value AS CHAR) LIKE '%Davenport%')
OR  (mt3.meta_key = 'geo_country' AND CAST(mt3.meta_value AS CHAR) LIKE '%United States%')
OR  (mt4.meta_key = 'geo_short_address' AND CAST(mt4.meta_value AS CHAR) LIKE '%Davenport%') ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10

请帮忙。

4 个答案:

答案 0 :(得分:5)

对于OR Query参数数组应该是这样的:

$args = array(
    'post_type' => 'post',
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'app_trailer-type',
            'value' => $job_tailor,
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'app_pay-type',
            'value' => $app_pay_type,
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'geo_address',
            'value' => $geo_address,
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'geo_country',
            'value' => $geo_country,
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'geo_short_address',
            'value' => $geo_short_address,
            'compare' => 'LIKE'
        ),
    )
);

参考:http://codex.wordpress.org/Class_Reference/WP_Query

答案 1 :(得分:4)

用类似的方式更改了args。

$args = array('post_type' => 'job_listing', 'relation' => 'OR');
$args['meta_query']=array(
    array(
        'key' => 'app_trailer-type',
        'value' => $job_tailor,
        'compare' => 'LIKE'
    ),
    array(
        'key' => 'app_pay-type',
        'value' => $app_pay_type,
        'compare' => 'LIKE'
    ),
    array(
        'key' => 'geo_address',
        'value' => $geo_address,
        'compare' => 'LIKE'
    ),
    array(
        'key' => 'geo_country',
        'value' => $geo_country,
        'compare' => 'LIKE'
    ),
    array(
        'key' => 'geo_short_address',
        'value' => $geo_short_address,
        'compare' => 'LIKE'
    ),
);

答案 2 :(得分:2)

您可以尝试这样的事情

$args = array(
    'post_type' => 'job_listing',
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'app_trailer-type',
            'value' => $job_tailor,
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'app_pay-type',
            'value' => $app_pay_type,
            'compare' => 'LIKE'
        ),
        // ....
    )
);

Read more on Codex.

答案 3 :(得分:0)

来自WP_Query类引用(link):

  

重要说明:tax_query采用一系列税务查询参数数组   (它需要一组数组) - 你可以在第二个例子中看到这一点   下面。此构造允许您使用查询多个分类   第一个(外部)数组中的关系参数来描述   分类法查询之间的布尔关系。

您应该在meta_query数组的开头添加array('relation' => 'OR')