0

我将自己的查询写入 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);

那么它将起作用。我做错了什么?

4

1 回答 1

1

select 表达式作为一个整体没有意义,因为你有矛盾的 where 条件。对于关系数据库的工作方式,它也没有任何意义。您想在一个查询中匹配两个共享相同列名的唯一行,如果不使用子查询之类的技术,这是不可能的。

考虑到所有表达式部分都必须为真,并且您得到如下内容:

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 >=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!

?>
于 2013-02-21T22:45:18.520 回答