WP_Query support and posts_per_page
-
Is there a way to have Ajax-Load-More work for a custom WP_Query that contains a posts_per_page parameter?
I have a WP_Query that contains ‘posts_per_page’ = > 5. When the page loads the first 5 queries are displayed as expected however when I click on the “Load More” button form ALM plugin there are only those 5 posts available in the ALM query.
I followed the approach answered here
I know that the problem is that ALM only knows about the first 5 queries based on the posts_per_page param in the WP_Query, but my question is, is there a way for ALM to take in the other WP_Query params and I start with an offset=5?
$news = new WP_Query ( array( 'post_type' => 'post', 'connected_type' => 'games2posts', 'connected_to' => $post->ID, 'posts_per_page' => 5 )); if ($news->have_posts()) { while ( $news->have_posts() ) : $news->the_post(); $post_ids[] = get_the_ID(); <?php } ?> <div class="entry"> <!-- start of entry --> <div class="row"> <div class="col-lg-12"> <a href="<?php the_permalink(); ?>"><img src="https://lorempixel.com/250/141/" alt="Image" class="img-responsive img-left"></a> <h4 class="nopadding"><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h4> <p class="tags"><?php echo 'Posted '; the_time('F j, Y'); ?> at <?php the_time('g:i a'); echo ' ('.time_ago().')'; ?> </p> <p><a href="<?php the_permalink(); ?>"><?php the_excerpt(); ?></a></p> <p class="cats"><strong>Filed Under:</strong> <?php the_category(', ') ?> </p> </div> </div> <!-- end of entry row --> </div> </a><!-- end of entry --> endwhile; if($post_ids) { //Implode the posts and set a variable to pass to our exclude param. $postsIn = implode(",", $post_ids); echo do_shortcode('[ajax_load_more repeater="template_2" post_type="post" button_label="Load More News" scroll="false" pause="false" transition="fade" post__in="'.$postsIn.'"]'); } } else { ?> <h4 class="center">No News for <?php the_title() ?></h4> <?php } ?>
I do have a work around, basically removing my div.entry and loading everything through ALM but this seems to be slower than if I load the initial, first 5 posts via code above.
This is my work around, where template_2 contains my <div class=”entry”>… </div>
$news = new WP_Query ( array( 'post_type' => 'post', 'connected_type' => 'games2posts', 'connected_to' => $post->ID, 'posts_per_page' => 5 )); if ($news->have_posts()) { while ( $news->have_posts() ) : $news->the_post(); $post_ids[] = get_the_ID(); endwhile; if($post_ids) { //Implode the posts and set a variable to pass to our exclude param. $postsIn = implode(",", $post_ids); echo do_shortcode('[ajax_load_more repeater="template_2" post_type="post" button_label="Load More News" scroll="false" pause="false" transition="fade" post__in="'.$postsIn.'"]'); } } else { ?> <h4 class="center">No News for <?php the_title() ?></h4> <?php } ?>
Sorry for long post & thanks in advance for any assistance.
- The topic ‘WP_Query support and posts_per_page’ is closed to new replies.