0

I need a flexible solution to put out a list of posts with comments. I'm trying to get this solution to work, but have no luck: https://wordpress.stackexchange.com/a/219216/91757

I'm always getting the normal list, as on front page of the blog. Even 'ignore_sticky_posts' => true doesn't work. What I'm doing wrong? Here's my code so far:

<?php function has_comments_join( $join, &$wp_query ) {
    global $wpdb;
    if ($comment_term = $wp_query->get('post_has_comments')) {
        $join .= $wpdb->prepare(" JOIN $wpdb->comments ON (($wpdb->posts.ID = $wpdb->comments.comment_post_ID) AND ($wpdb->comments.comment_type = %s) AND ($wpdb->comments.comment_approved = %s)) ", $comment_term['comment_type'], $comment_term['comment_status']);
    }
    return $join;
}

$results = new WP_Query(array(

    'post_type'             => array( 'post' ),
    'ignore_sticky_posts'   => true,
    'posts_per_page'        => 8,
    'order'                 => 'dsc',

    'post_has_comments' => array(
        'comment_type' => 'review',
        'comment_status' => '1'
    )
)); ?>  

<?php while ( $results->have_posts() ): $results->the_post(); ?>
    <?php get_template_part( 'parts/loop', 'archive' ); ?>
<?php endwhile; ?>

Everything is working now, except pagination:

<?php   
function my_has_comments_filter( $where ) {
    $where .= ' AND comment_count > 0 ';
    return $where;
}
add_filter( 'posts_where', 'my_has_comments_filter' );
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$query = new WP_Query( array(
    'posts_per_page'        => 8,         
    'paged'                 => $paged,
    'ignore_sticky_posts'   => true,
    'order'                 => 'dsc',
) );
// Don't filter future queries.
remove_filter( 'posts_where', 'my_has_comments_filter' ); ?>

<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

    <?php get_template_part( 'parts/loop', 'archive' ); ?>

<?php endwhile; ?>
    <?php joints_page_navi(); ?> // Does not work with original WP code too
    <?php wp_reset_postdata(); ?>

<?php else : ?>

    <?php get_template_part( 'parts/content', 'missing' ); ?>

<?php endif; ?>
5
  • wait.. where you put the whole of your code?
    – Jevuska
    Commented Apr 5, 2016 at 7:03
  • I mean in your theme files, which file?
    – Jevuska
    Commented Apr 5, 2016 at 9:12
  • Ah okay, sorry. I use JointsWP (github.com/JeremyEnglert/JointsWP) and copied the archive.php for creating a custom category template.
    – anyway
    Commented Apr 5, 2016 at 9:20
  • You should not be running custom queries in place of the main query. They are always troublesome. Use pre_get_posts to alter the main query Commented Apr 5, 2016 at 9:51
  • @Pieter Goosen Thanks for your input. At the moment I don't really know how to work with pre_get_posts, but I'll deal with it when I have time to study the WP Codex page about the topic.
    – anyway
    Commented Apr 5, 2016 at 11:16

1 Answer 1

2

If you'd like to change the SQL query for a WP_Query, you're going to need to use the posts_join filter, or in your case, it's easier to go for a simple posts_where:

function my_has_comments_filter( $where ) {
    $where .= ' AND comment_count > 0 ';
    return $where;
}

add_filter( 'posts_where', 'my_has_comments_filter' );
$query = new WP_Query( array(
    'posts_per_page' => 10,
) );

// Don't filter future queries.
remove_filter( 'posts_where', 'my_has_comments_filter' );

Hope that helps!

5
  • Thanks a lot for your quick reply. That works! But what's not working is ignore_sticky_post and pagination. I put my new code under the first one above.
    – anyway
    Commented Apr 4, 2016 at 16:07
  • I think it will work if you remove query_posts. Also, don't put remove_filter into your while loop, put it right at the end of your WP_Query. Finally, if you're going to remove query_posts you should also remove wp_reset_query(), but still use wp_reset_postdata().
    – kovshenin
    Commented Apr 4, 2016 at 16:16
  • Okay, now I'm a bit confused, because I have no output anymore since I removed the WP_Query. I'm afraid, that I'm not really understand the whole thing. Again the new code above.
    – anyway
    Commented Apr 4, 2016 at 16:55
  • have_posts() and the_post() work on the global $wp_query object. Since you have a new local $query object you should use $query->have_posts() and $query->the_post() instead.
    – kovshenin
    Commented Apr 4, 2016 at 17:29
  • Thanks a lot for your patience and help me learning! Everything is working now, except the pagination. In my code above I'm using a snippet of my template for pagination, but the original wordpress code does not work too. But if you say, that my code is correct now, I can control the length of the list with the WP_Query array parameters too.
    – anyway
    Commented Apr 5, 2016 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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