Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 59525

Custom WP Query Help

$
0
0

Replies: 0

Hi! Thanks for your great plugin. I’ve used code from this post to grab posts labeled in the same category as the page a user is on (e.g., if user is on strategy it pulls blog posts with the category “strategy”). https://wordpress.org/support/topic/ajax-load-more-for-related-posts/

The query works and the Ajax load more plugin loads additional posts in the correct category. However, the loaded posts are limited to 5 posts total no matter how many posts are in the same category. When I inspect, I see that data-total-posts = “5” no matter what. Here’s my custom query that is located in my functions.php file.

/* RELATED BLOG POSTS SHORTCHODE */
function services_related_posts($args = array()) {
    global $post;

	$terms = get_the_terms( $post->ID , 'category', 'string');
			//Pluck out the IDs to get an array of IDS
			$term_ids = wp_list_pluck($terms,'term_id');
					$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

     // query
    $related_blogs = get_posts(array(
        'post_type' => 'post',
			    'tax_query' => array(
                    array(
                        'taxonomy' => 'category',
                        'field' => 'id',
                        'terms' => $term_ids,
                        'posts_per_page'      => -1, // get all posts
                        'operator'=> 'IN' //Or 'AND' or 'NOT IN'
                     )),
    
			      'post__not_in'=>array($post->ID),
			      			      'paged' => $paged

    ));

$post_in = array();
foreach ( $related_blogs as $post ){ 
   setup_postdata( $post );
   $post_in[] = $post->ID; // Store post IDs
}
wp_reset_query();

echo do_shortcode('[ajax_load_more id="5448190871" button_label="Load More" scroll="false" posts_per_page="3" sticky_posts="true" post__in="'.implode(',', $post_in).'"]');

}

Viewing all articles
Browse latest Browse all 59525

Trending Articles