Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've looked at numerous examples of wp_query and can't figure this out. If the thing I was comparing were a meta value then I could to a meta query, but apparently you can't do a compare with a non-meta value?

This is what I thought it would work:

$commentpost = new WP_Query( array( 'key' => 'comment_count',
                                'value' => '0',
                                'compare' => '>',                                                    
                                'orderby' => 'comment_count',
                                'order' => 'DESC', 
                               ) );

Any help would be appreciated.

share|improve this question
    
I wrote a patch the other night to be able to query posts based on amounts of comments, please test it over here: core.trac.wordpress.org/ticket/28399 –  Ramon Fincken Jun 14 at 20:57

1 Answer 1

up vote 1 down vote accepted

While the WP_Query class has the native possibility to orderby the comment_count, it doesn't have the same to query based on those. But when we look at the posts-table, we can see that there isn't much we need to alter

ID | post_author | post_date | post_date_gmt | post_content | post_title | post_excerpt | post_status | comment_status | ping_status | post_password | post_name | to_ping | pinged | post_modified | post_modified_gmt | post_content_filtered | post_parent | guid | menu_order | post_type | post_mime_type | comment_count

So the thing we could do, is intercept the query and modify the WHERE clause inside the posts_where-filter.

<?php
defined( 'ABSPATH' ) or exit;
/* Plugin Name: (#121083) Query WHERE comment count not is 0 */

add_filter( 'posts_clauses', 'wpse121083WhereCommentCountNotNull' );
function wpse121083WhereCommentCountNotNull( $where )
{
    // Don't fire more than once:
    remove_filter( current_filter(), __FUNCTION__ );

    # @TODO Add abort clauses/cases here

    return "{$where} AND {$GLOBALS['wpdb']->posts}.comment_count != 0";
}

You still have to fill in the bits like cases where you don't want to intercept the query. Keep in mind, that it's not tested.

share|improve this answer
    
Thanks. I'm a bit new to all this. I'm not knocking your answer, but I'm using a filter currently to return posts only within the past 20 days. WP3.7 allows for a better method as I understand it with that. Is using filters like this considered a kludge? Is it considered best practice to just write out a full SQL statement for what you want rather than appending stuff to a wp_query via a filter? –  Patrick Johnson Nov 3 '13 at 20:51
    
You could write a separate custom SQL statement as well, but then you'd simply drop the main query result that runs anyway and add another query on top. So best practice would be to intercept the main query - if it's the main query. If it's not the main query, you can do whatever helps you to reach your goal (as long as it doesn't involve the use of query_posts()). –  kaiser Nov 3 '13 at 20:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.