が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でコードを組み合わせるホーボー
あなたのアップデートは(posts_joinではなく 'posts_where'で)良く見えます。 '$ wpdb-> order_history'をどこかに定義しましたか?そうでない場合は、 '{$ wpdb-> prefix} order_history'かそれに似たものに変更する必要があります。返されるフィールドのリストに 'c.Cnt'を追加するために' posts_fields'フィルタが必要な場合もあります。 – Hobo
Hmm - あなたの最後の更新は、私のコメントが参照されなくなったことを意味します:(...とにかく、ここにあります:https://codex.wordpress.org/Query_Overview、 "What Plugins Modify"の下に – Hobo
私の友人、posts_fieldsフィルターを返すフィールドのリストにc.Cntを追加する方法をありがとう、あなたは私に例:ありがとう:) –