Note that there are some explanatory texts on larger screens.

plurals
  1. POWordpress wp_query filter using posts_groupby - how to SUM values in joined table?
    primarykey
    data
    text
    <p>Can somebody please help me with a crazy wordpress problem? I'm using wordpress's add_filter to join a second table for my wp_query and I'm trying to get the query to group_by a pageviews column (from a plugin I installed) and SUM them for my output. I'm managing to get the results grouped but the result set only shows one of the values. I would like to know 2 things.</p> <ol> <li>Is my join ok for joining the posts table to the wp_popularpostsdata table where the wp_popularpostsdata table will have more than one entry per post->ID?</li> <li>How do I use the SUM function as I can't see it anywhere in the Codex?</li> </ol> <p>I'm having to use wp_query, as opposed to query_posts, as that is what is used for the rest of the output in my client's templates. I tried using wppp.postid as my groupby but it returned no results. Here's my code, hopefully it makes a bit more sense.</p> <p>Functions in my site plugin:-</p> <pre><code> function add_filter_7dayspopular(){ add_filter('posts_where', 'edit_posts_where'); add_filter('posts_join','edit_posts_join'); add_filter('posts_groupby','edit_posts_groupby'); add_filter('posts_orderby','edit_posts_orderby'); function edit_posts_join($join_statement) { global $wpdb; remove_filter( current_filter(), __FUNCTION__ ); $join_statement .= "LEFT JOIN wp_popularpostsdata wppp ON $wpdb-&gt;posts.ID = wppp.postid"; return $join_statement; } function edit_posts_where($where_statement) { global $wpdb; remove_filter( current_filter(), __FUNCTION__ ); $where_statement .= " AND wppp.last_viewed &gt; '" . date('Y-m-d', strtotime('-7 days')) . "'"; return $where_statement; } function edit_posts_groupby($groupby_statement) { global $wpdb; remove_filter( current_filter(), __FUNCTION__ ); $groupby_statement = "{$wpdb-&gt;posts}.ID"; return $groupby_statement; } function edit_posts_orderby($orderby_statement){ global $wpdb; remove_filter( current_filter(), __FUNCTION__ ); $orderby_statement = "wppp.pageviews DESC"; return $orderby_statement; } } </code></pre> <p>Here's my new wp_query code:-</p> <pre><code> add_filter_7dayspopular(); $args = array( 'post_type' =&gt; array( 'post', 'portfolio' ), 'post__not_in' =&gt; $itemArray, 'posts_per_page' =&gt; $load_more, 'post_status'=&gt;'publish', 'orderby' =&gt; 'pageviews', 'order' =&gt; 'DESC', 'paged' =&gt; $paged ); </code></pre> <p>Thanks guys.</p> <p>Mark</p> <p>Loushou answered the question fully. After running the code the field $post_.total_views is available to use inside my loop. Here's their code with a tiny change by myself:-</p> <pre><code> // change your orderby function to order by the sum function edit_posts_orderby($orderby_statement){ global $wpdb; remove_filter( current_filter(), __FUNCTION__ ); // add sum() around the field name $orderby_statement = 'sum(wppp.pageviews) DESC'; return $orderby_statement; } // add a posts_fields function function edit_posts_fields($fields_statement) { global $wpdb; remove_filter( current_filter(), __FUNCTION__ ); $fields_statement .= ', sum(wppp.pageviews) as total_views'; return $fields_statement; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload