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条)

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器