2016-08-10 66 views
-1

我試圖選擇帖子標題和兩個metafield值。但我不能得到正確的查詢.Y查詢被粘貼在下面請幫助。wpdb查詢加入帖子和帖子meta

$querystr = " 
       SELECT $wpdb->posts.post_title 
       FROM $wpdb->posts, $wpdb->postmeta 
       WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
       AND $wpdb->postmeta.meta_key = side_post_meta_id 
       AND $wpdb->postmeta.meta_value LIKE %".$search_term."% 
       AND $wpdb->postmeta.meta_key = side_post_id 
       AND $wpdb->postmeta.meta_value LIKE %".$cat_find."%     
       AND $wpdb->posts.post_status = 'publish' 
       AND $wpdb->posts.post_type = 'post' 
       ORDER BY $wpdb->posts.post_date DESC LIMIT 0,10 
      "; 
      print_r($querystr); 
      $pageposts = $wpdb->get_results($querystr, OBJECT); 
+1

歡迎堆棧溢出。請閱讀以下關於如何在本網站上提出問題的文章:http://stackoverflow.com/help/how-to-ask。至少請提供您的表格結構,您的期望結果和實際結果。 –

回答

0

請嘗試這樣的事情......

<?php 
    global $wpdb; 
    $get_results= $wpdb->get_results("SELECT p.post_title, pm2.meta_value , pm3.meta_value 
        FROM wp_posts as p 
    INNER JOIN wp_postmeta AS pm1 ON pm1 .post_id = p.ID 

    INNER JOIN wp_postmeta AS pm2 ON pm2 .post_id = p.ID AND pm2.meta_key='side_post_meta_id' AND pm2.meta_value like '%$search_term%' 

    INNER JOIN wp_postmeta AS pm3 ON pm3 .post_id = p.ID AND pm3 .meta_key='side_post_id' AND pm3 .meta_value like '%$cat_find%' 

    WHERE p.post_status = 'publish' AND p.post_type = 'post' ORDER BY p.post_date DESC LIMIT 0,10"); 

    echo '<pre>'; 
    print_r($get_results); 
    echo '</pre>'; 
+0

OP的問題是什麼?你的代碼如何解決它? – showdev

+0

那麼你可以給我一個解決方案,因爲你知道什麼是問題 – Shafi

+0

我不明白你的答覆。 – showdev

相關問題