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

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?