使用多个元键和值获取POST

时间:2016-09-02 作者:Sheraz Ahmed

我正在尝试根据以下元键获取帖子。

  • post_code 具有432C
  • location 具有XYZ两者都属于CPT。我试图用这两个meta\\u值获取帖子
我不想要OR关系,我想要AND关系,我已经尝试了几个WP_Query 对象,但经过数小时的查找仍没有找到解决方案。

6 个回复
最合适的回答,由SO网友:Sheraz Ahmed 整理而成

SOLUTION

下面接受的解决方案有效,但是,我想知道它是如何工作的?

这就是它的工作原理

SELECT * FROM wp_posts p, wp_postmeta m1, wp_postmeta m2
    WHERE p.ID = m1.post_id and p.ID = m2.post_id
    AND m1.meta_key = \'key1\' AND m1.meta_value = \'value1\'
    AND m2.meta_key = \'key2\' AND m2.meta_value =  \'value2\'
    AND p.post_type = \'cpt\' AND p.post_status = \'published\'

SO网友:Jeremy Ross

这应该可以做到。默认关系为和,因此无需指定。

$args = array(
    \'post_type\'  => \'wpse_cpt\',
    \'meta_query\' => array(
        array(
            \'key\'     => \'post_code\',
            \'value\'   => \'432C\',
        ),
        array(
            \'key\'     => \'location\',
            \'value\'   => \'XYZ\',
        ),
    ),
);
$query = new WP_Query( $args );

SO网友:Chiranjib Khanra
$args = array(
    \'post_type\'  => \'wpse_cpt\',
    \'meta_query\' => array(
        \'relation\' => \'AND\' //**** Use AND or OR as per your required Where Clause
        array(
            \'key\'     => \'post_code\',
            \'value\'   => \'432C\',
        ),
        array(
            \'key\'     => \'location\',
            \'value\'   => \'XYZ\',
        ),
    ),
);
$query = new WP_Query( $args );
SO网友:Eric Shoberg

再次编辑:啊,我没有回答这个问题。您可以选择a.* 获取具有匹配键值的帖子的所有列,而不是选择c.meta_value.

下面的sql/$wpdb 查询将选择所有meta\\u值%s post\\u类型为的帖子的$postType 和meta\\u键值$metaKey. 因此,选择给定meta\\u键的所有不同值(通过“$metaKey”)。这个term_relationships 表是wordpress的帮助表,用于表中的关系连接。wp_posts 是wordpress的“posts”表,wp_postmeta 是wordpress的“Posteta”表。(注意:如果使用的是自定义表,则这些表的名称会有所不同。)

~根据@MikeNGarrett的要求,进行编辑以添加“正在做”的笔记

/* $wpdb is a global var provided by the wordpress \'engine\'
** for query\'ing wordpress database tables. 
*/
global $wpdb; 
$postType = \'mycustomposttype\';
$metaKey = \'mymetakey\';
$query = "
    SELECT c.meta_value 
    FROM wp_posts a 
    LEFT JOIN wp_term_relationships b 
        ON (a.ID = b.object_id) 
    LEFT JOIN wp_postmeta c 
        ON (a.ID = c.post_id) 
    WHERE a.post_type = %s AND c.meta_key = %s";

$metaValues = $wpdb->prepare(query, [$postType, $metaKey]);
$metaValues = $wpdb->get_results($metaValues);
注意:我只是重用$metaValues变量。没有其他理由写入$元值的结果prepare() 返回$metaValues变量。但是,您必须将$元值传递给get_resluts().

SO网友:breadwild

我尝试使用上面建议的WP\\u查询解决方案,但结果为空。谢拉兹·艾哈迈德建议的方法效果很好。以下是我对其价值的看法:

//database
wp_posts
+---------+--------------------+--------------+
|  ID     | post_title         | post_type    |
+---------+--------------------+--------------+
| 8567    | Intro to MySQL     | talk         |
+---------+--------------------+--------------+
| 8590    | Intro to PHP       | talk         |
+---------+--------------------+--------------+

wp_postmeta
+---------------+------------+----------------+
|  post_id      | meta_key   | meta_value     |
+---------------+------------+----------------+
| 8567          | speaker    | John Doe       |
+---------------+------------+----------------+
| 8567          | year_of    | 2021           |
+---------------+------------+----------------+
| 8590          | speaker    | John Doe       |
+---------------+------------+----------------+
| 8590          | year_of    | 2021           |
+---------------+------------+----------------+


//search by speaker and Year
$speaker = "John Doe";
$year_of = "2021";

$talks = $wpdb->get_results(  
     $wpdb->prepare( 
         \'SELECT post_title, meta_value AS speaker 
         FROM \'.$wpdb->prefix.\'posts AS pt, \'.$wpdb->prefix.\'postmeta AS pm 
         WHERE post_type = %s AND pm.post_id = pt.id AND ( meta_key = %s AND meta_value = %s ) 
         AND post_id IN ( SELECT post_id FROM nm_postmeta WHERE meta_key = %s AND meta_value = %s )\',
         array( \'talk\', 
                \'speaker\', 
                $speaker, 
                \'year_of\', 
                $year_of )
    )
);
                
foreach ( $talks as $talk ) {
    echo "<p>".$talk->speaker.\': \'.$talk_homily->post_title."</p>";
}
John Doe:MySQL简介

约翰·多伊:PHP简介

SO网友:Manthan Dave

尝试以下对象参数

array(
            \'key\' => \'post_code\',
            \'value\' =>\'432C\',
            \'compare\' => \'=\'
        ),
        array(
            \'relation\' =>\'AND\',
             array(

            \'key\' => \'location\',
            \'value\' => \'XYZ\',
             \'compare\' => \'=\',

          ),
        )

相关推荐

如何在wp-Query中比较不同的时间戳以获取事件自定义帖子类型?

我有一个带有自定义元数据库的自定义帖子类型(as seen on wptheming.com) 并且希望进行查询以显示即将发生的事件,例如在侧边栏中,过去的日期被忽略。但我需要一个当前时间的值来和事件开始时间进行比较。现在我从current_time(\'mysql\') 看起来是这样的:2012-02-16 13:15:31元的开始时间如下所示:201108121100如何使这两个日期具有可比性?我可以在查询中执行吗?或者是否有其他解决方案?以下是我到目前为止的查询(值留空,时间戳回显):<?ph