Add "Category" to Mysql Query

时间:2014-06-26 作者:Caponi Elia

我需要你的帮助:)

我在WordPress上进行了以下自定义Mysql查询:

    $querystr = "
   SELECT  DISTINCT meta_value  FROM $wpdb->postmeta WHERE post_id IN(     
   SELECT DISTINCT wpostmeta.post_id
   FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
   WHERE wposts.ID = wpostmeta.post_id 
   AND wpostmeta.meta_key  = \'color\' 
   AND wpostmeta.meta_value  = \'red\' 
   AND wposts.post_type = \'post\' ) AND meta_key =\'size\' 
   ORDER BY RAND()
   LIMIT 4
   ";
如何在查询搜索中添加“类别名称”或“类别ID”?

干杯;)

2 个回复
SO网友:Nicolai Grossherr

如果我没有遗漏什么,下面的内容应该对你有用。从Codex: WP_Query - Custom Field Parameters. 这样做:

$args = array(
    \'post_type\' => \'post\',
    \'meta_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'key\' => \'color\',
            \'value\' => \'red\',
            \'compare\' => \'=\'
        ),
        array(
            \'key\' => \'size\',
            \'compare\' => \'EXISTS\'
        )
    )
);
$query = new WP_Query( $args );
此处的其他资源:

SO网友:TBI Infotech

要在查询搜索中添加“类别名称”或“类别ID”,必须应用内部联接(wp\\U term\\U关系)。

请试试这个

$category\\u id是您的类别id

<?php 

$args = array(
    \'post_type\' => \'post\',
    \'meta_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'key\' => \'color\',
            \'value\' => \'red\',
            \'compare\' => \'=\'
        ),
        array(
            \'key\' => \'size\',
            \'compare\' => \'EXISTS\'
        )
    ),
    \'tax_query\' => array(
                            \'relation\' => \'AND\',
                array(
                    \'taxonomy\' => \'category\',
                    \'field\' => \'id\',
                    \'terms\' => $category_id,
                    \'operator\'  => \'IN\'
                )),
);
$query = new WP_Query( $args );

?>

结束

相关推荐

为什么dbDelta()不能捕获MysqlErrors?

据我所见,dbDelta() 用于抑制在其操作过程中发生的数据库错误。一般来说,情况似乎是这样,但New Relic仍在报告函数中的MysqlErrors。准确的错误消息格式如下:MysqlError: Table \'xxx.wp_yyy_posts\' doesn\'t exist 发件人dbDelta() 在里面/wp-admin/includes/upgrade.php, 我们有:// Fetch the table column structure from the database&