簡體   English   中英

使用posts_groupby的WordPress wp_query過濾器-如何計算聯接表中的值?

[英]Wordpress wp_query filter using posts_groupby - how to COUNT values in joined table?

嘗試將我的自定義表加入WP_Query,這是我的wp_query代碼

     $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(); 

我看到在線教程說,在那里我可以使用WordPress過濾器使用posts_join,posts_group,posts_where將自定義表加入wp_query。

我的問題是有一個自定義表,該表調用wp_order_history

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

有我要合並到wp_query的SQL代碼

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)

在這里我嘗試使用posts_where過濾器,但不起作用:(

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->總計?

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;

    }

解決問題,謝謝Hobo

將代碼與我的注釋結合在一起(注意:join函數中的內部查詢與您的內部查詢不同-您發布的示例表沒有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";
}

然后可以在全局$post變量上訪問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;
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM