用post_meta搜索

时间:2013-02-21 21:48:03

标签: php sql wordpress

我将自己的查询写入wordpress数据库并且我被卡住了。我有很多(5)自定义字段,如:

town
price
size
... etc

在search.php中我有:

$querystr = "
SELECT wposts.* 
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id ";

$town = addslashes($_GET['town']);
if($town!=''){
    $querystr .= " AND wpostmeta.meta_key = 'town' AND wpostmeta.meta_value = '".$town."'";
}

$mo = addslashes($_GET['mo']);
if($mo!='' && preg_match("/^\d+$/", $mo)){
    $querystr .= " AND wpostmeta.meta_key = 'price' AND wpostmeta.meta_value > '".$mo."'";
}

$md = addslashes($_GET['md']);
if($md!='' && preg_match("/^\d+$/", $md)){
    $querystr .= " AND wpostmeta.meta_key = 'price' AND wpostmeta.meta_value < '".$md."'";
}

$querystr .= " AND wposts.post_status = 'publish' AND wposts.post_type = 'post'";

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

但这不起作用。如果我只使用一个条件:

$querystr = "SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id ";

$town = addslashes($_GET['town']);
if($town!=''){
    $querystr .= " AND wpostmeta.meta_key = 'town' AND wpostmeta.meta_value = '".$town."'";
}

$querystr .= " AND wposts.post_status = 'publish' AND wposts.post_type = 'post'";
$pageposts = $wpdb->get_results($querystr, OBJECT);
然后它会起作用。我做错了什么?

1 个答案:

答案 0 :(得分:1)

select表达式作为一个整体没有意义,因为你在条件中有矛盾。关于关系数据库的工作方式,它也没有意义。您希望匹配在一个查询中共享相同列名的两个唯一行,如果不使用subqueries之类的技术,则无法进行此操作。

考虑所有表达式部分必须为真,你得到类似的东西:

SELECT wposts.*
FROM wp_posts wposts, wp_postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'town' AND wpostmeta.meta_value = 'My town' 
AND wpostmeta.meta_key = 'price' AND wpostmeta.meta_value > 500

在这里你说meta_key等于“城镇”而meta_value等于“我的城镇”。这是有道理的,但是当你还说meta_key 等于“价格”时,meta_value 大于500.表达式永远不会是是的,无论解析器也无法将两个不同的条件集合在一起。

WP_Query

如果可能,我建议您使用WP_Query类,而不是直接查询数据库。这个包装器大大简化了代码并使其更易于维护。请注意,代码需要WordPress&gt; = 3.1,因为它使用meta_query选项。

您的查询可以这样写:

<?php

// The arguments that defines the query
$args = array(
    'post_status' => 'publish',
    'post_type' => 'post'
);

// We define the meta/custom field conditions

$meta_query = array();

// PS: No need to slash the values, WordPress will do that for us
$town = $_GET['town'];
$mo = (int) $_GET['mo']; // Simple sanitizment, implement your own as see fit
$md = (int) $_GET['md']; // Simple sanitizment, implement your own as see fit


if ( $town ) {
    $meta_query[] = array(
        'key' => 'town',
        'value' => $town
    );
}

if ( $mo ) {
    $meta_query[] = array(
        'key' => 'price',
        'value' => $mo,
        'type' => 'NUMERIC',
        'compare' => '>'
    );
}

if ( $md ) {
    $meta_query[] = array(
        'key' => 'price',
        'value' => $md,
        'type' => 'NUMERIC',
        'compare' => '<'
    );
}

if ( $meta_query ) {
    // Add the meta_query conditions to the arguments array

    $meta_query['relation'] = 'AND';

    $args['meta_query'] = $meta_query;
}

// Create WP_Query object with the arguments
$query = new WP_Query( $args );

// Fetch the posts
$posts = $query->get_posts();

// Voila!

?>