带有 Wordpress 元值的 MySQL 查询 [英] MySQL Query with Wordpress Meta values

查看:30
本文介绍了带有 Wordpress 元值的 MySQL 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些带有多个元值的 wordpress 帖子...

I have a few wordpress posts with multiple meta values...

我正在尝试编写一个查询来查找所有具有 X 值的帖子...

Im trying to write a query that will find all posts with X values...

$customkey1 = 'Type of Vehicle';
$customvalue1 = $_POST['OPT1']; 

$customkey1 = 'Network'; 
$customvalue1 = $_POST['OPT2']; 

$my_posts = $wpdb->get_results("
    SELECT * FROM $wpdb->posts, $wpdb->postmeta 
    WHERE ID = $wpdb->postmeta.post_id 
    AND meta_key = '$customkey' 
    AND meta_value = '$customvalue' 
    AND meta_key = '$customkey1' 
    AND meta_value = '$customvalue1' 
    AND $wpdb->posts.post_status = 'publish' 
    ORDER BY post_date DESC
");

我认为我出错的地方是我说 'WHERE meta_key' 等于 1 件事,但我说如果它等于另一件事,我需要看看是否有任何元键相同,这有意义吗?

I think where im going wrong is im saying 'WHERE meta_key' equals 1 thing but then im saying if it equals another, I need to see if any of the meta keys are the same, does this make sense?

谢谢

推荐答案


SELECT * FROM $wpdb->posts as posts, $wpdb->postmeta as postmeta1 , $wpdb->postmeta as postmeta2 (and more....)
WHERE
posts.ID = postmeta1.post_id
posts.ID = postmeta2.post_id
(and more....)
AND postmeta1.meta_key = '$customkey'
AND postmeta1.meta_value = '$customvalue'
AND postmeta2.meta_key = '$customkey1'
AND postmeta2.meta_value = '$customvalue1'
(and more....)
AND $wpdb->posts.post_status = 'publish'
ORDER BY post_date DESC

这篇关于带有 Wordpress 元值的 MySQL 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆