Wordpress - 按多个具有相同值的元键过滤帖子

时间:2015-03-16 15:00:53

标签: php wordpress wp-query

LOOP:

<?php
$meta[] = array(
    'key' => 'myfield1111',
    'value' => 'myvalue', //same value, it exists in this field
    'compare' => 'LIKE'
);
$meta[] = array(
    'key' => 'myfield2222',
    'value' => 'myvalue', //same value, id doesn't exists in this filed
    'compare' => 'LIKE'
);

$loop = new WP_Query(
    array(
        'post_type' => 'myposttype',
        'posts_per_page' => 24,
        'paged' => $paged,
        'meta_query' => array(
        'relation' => 'OR',
        $meta
    ),
));
?>

这是空查询(没有回复帖子)的结果,即使值myvalue退出密钥myfield1111。这对我来说不合逻辑,因为我使用OR关系进行元查询。

为什么会发生这种情况,我该如何解决?

1 个答案:

答案 0 :(得分:0)

解决!

<?php
$querystr = "
    SELECT $wpdb->posts.* 
    FROM $wpdb->posts, $wpdb->postmeta
    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
    AND ($wpdb->postmeta.meta_key = 'myfield1111' AND $wpdb->postmeta.meta_value LIKE '%myvalue%')
    OR ($wpdb->postmeta.meta_key = 'myfield2222' AND $wpdb->postmeta.meta_value LIKE '%myvalue%')
    AND $wpdb->posts.post_status = 'publish' 
    AND $wpdb->posts.post_type = 'myposttype'
    AND $wpdb->posts.post_date < NOW()
    ORDER BY $wpdb->posts.post_date DESC
";

$pageposts = $wpdb->get_results($querystr, OBJECT);              
?>