简体   繁体   English

使用posts_groupby的WordPress wp_query过滤器-如何计算联接表中的值?

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

try to join my custom table to WP_Query, here is my wp_query code 尝试将我的自定义表加入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(); 

I saw the online tutorial said there I can join my custom table to the wp_query, using posts_join, posts_group, posts_where WordPress filter. 我看到在线教程说,在那里我可以使用WordPress过滤器使用posts_join,posts_group,posts_where将自定义表加入wp_query。

my problem is having a custom table, which calls wp_order_history 我的问题是有一个自定义表,该表调用wp_order_history

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

there is the SQL code I want to merge to wp_query 有我要合并到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)

here I try to usings posts_where filter but not working :( 在这里我尝试使用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');

The result I want to get in the wp_query loop, count the total of orders each business have. 我想要进入wp_query循环的结果,计算每个企业的订单总数。

UPDATE, seem like code running through, but how can i output this? 更新,似乎正在运行的代码,但我怎么能输出呢? inside the wp_query loop? 在wp_query循环中? $the_query->total? $ 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;

    }

problem solve, thanks Hobo 解决问题,谢谢Hobo

Combining your code with my comments (NB: the inner query in the join function is different to yours - the example table you posted doesn't have a business_id column): 将代码与我的注释结合在一起(注意: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";
}

The value of the total column can then be accessed on the global $post variable. 然后可以在全局$post变量上访问total$post值。 For example, in the loop: 例如,在循环中:

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