2016-10-03 5 views
0

がWP_Queryに私のカスタムテーブルに参加しよう、ここで私は、オンラインチュートリアルでは、 はposts_join、posts_groupを使用して、私はwp_queryに私のカスタムテーブルに参加することができますが言った見たのWordPressフィルタをposts_where私wp_queryコードposts_groupbyを使用しているWordPress wp_queryフィルタ - 結合テーブルの値をCOUNTする方法はありますか?

 $the_query = new WP_Query(array('post_type'=> $post_type, 'paged' => $paged ,'posts_per_page' => 1, 'meta_query' => $meta_query, 'tax_query' => $tax_query, 'post_status' => 'publish', 'follower_id' => '')); 

if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); 

です。

私の問題は、私はここに

LEFT JOIN(Select count(post_id), post_id as total_order From wp_order_history as oh WHERE oh.post_id = $wpdb->post_id group by post_id) 

をwp_queryするマージするSQLコードがあり、私はposts_whereフィルタをusingsしようとしたが動作していないwp_order_history

ID  post_id  order_number 
1  211   AFD123D342234 
2  211   dsafa23411414 
3  110   sdafsaf234234 
4  211   sdafasdfadsfs 

を呼び出す、カスタムテーブルを持っています:(

function custom_posts_where($where) { 
      global $wpdb; 
      $table_name = $wpdb->prefix . 'order_history'; 
      $where .= $wpdb->prepare("LEFT JOIN(Select count(post_id), post_id as total_order From wp_order_history as oh WHERE oh.post_id = $wpdb->post_id group by post_id)"); 
      return $where; 
     } 
add_filter('posts_where', 'custom_posts_where'); 

結果私はwp_queryループを取得したい、各事業には注文があります。

更新は、コードが実行されているようですが、これをどのように出力できますか? wp_queryループの中に? $ the_query-> total?

add_filter('posts_join', 'custom_posts_join', 10, 2); 
    function custom_posts_join($join, $query) { 

     global $wpdb; 
     //* if main query and search... 
     // if (is_main_query() && is_search()) { 

      //* join term_relationships, term_taxonomy, and terms into the current SQL where clause 
      $join .= " 
       LEFT JOIN 
        (SELECT count(b.post_id) as total, b.post_id FROM wp_order_history as b group by b.business_id) as c 
       ON c.post_id = $wpdb->posts.ID "; 

     // } 
     return $join; 

    } 

問題が解決し、おかげで私のコメント(NBでコードを組み合わせるホーボー

+0

あなたのアップデートは(posts_joinではなく 'posts_where'で)良く見えます。 '$ wpdb-> order_history'をどこかに定義しましたか?そうでない場合は、 '{$ wpdb-> prefix} order_history'かそれに似たものに変更する必要があります。返されるフィールドのリストに 'c.Cnt'を追加するために' posts_fields'フィルタが必要な場合もあります。 – Hobo

+0

Hmm - あなたの最後の更新は、私のコメントが参照されなくなったことを意味します:(...とにかく、ここにあります:https://codex.wordpress.org/Query_Overview、 "What Plugins Modify"の下に – Hobo

+0

私の友人、posts_fieldsフィルターを返すフィールドのリストにc.Cntを追加する方法をありがとう、あなたは私に例:ありがとう:) –

答えて

1

:参加機能では、内側のクエリはあなたに異なっている - あなたが投稿business_idを持っていない例テーブルカラム):

add_filter('posts_join', 'custom_posts_join', 10, 2); 
function custom_posts_join($join, $query) { 

    global $wpdb; 
    //* if main query and search... 
    // if (is_main_query() && is_search()) { 

     //* join term_relationships, term_taxonomy, and terms into the current SQL where clause 

     $join .= " 
      LEFT JOIN 
       (SELECT count(*) as total, b.post_id FROM {$wpdb->prefix}order_history as b group by b.post_id) as c 
      ON c.post_id = $wpdb->posts.ID "; 

    // } 
    return $join; 
} 

add_filter('posts_fields', 'custom_posts_fields'); 
function custom_posts_fields($sql) { 
    // c matches the table alias used in custom_posts_join(). 
    // The ifnull() function makes sure we have a value of 0 if the joined post is not in wp_order_history 
    return $sql . ", ifnull(c.total,0) as total"; 
} 

totalカラムの値は、グローバル$post変数にアクセスすることができます。例えばループの中で:

<?php 
global $post; 
if (have_posts()) : 
    while (have_posts()) : the_post(); 
     print_r($post->total); // Field name should match the column name in the custom_posts_fields() function 
    endwhile; 
endif; 
?> 
+0

ありがとう、私の友人、コードは働いています、今私はposts_join、post_fields、post_whereの仕事を知っています、もう一度私の友人に感謝します:-) –

+0

問題はありません。お力になれて、嬉しいです – Hobo

関連する問題