doushihu5475 2012-03-21 10:47
浏览 56
已采纳

使用Wordpress Meta值进行MySQL查询

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

enter image description here

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
");

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?

Thanks

  • 写回答

4条回答 默认 最新

  • duan62819774 2012-03-21 14:26
    关注


    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
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示