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; ?>
pre_get_posts
to alter the main querypre_get_posts
, but I'll deal with it when I have time to study the WP Codex page about the topic.