Title: Custom WP Query Help
Last modified: May 21, 2019

---

# Custom WP Query Help

 *  Resolved [doctorlam1982](https://wordpress.org/support/users/doctorlam1982/)
 * (@doctorlam1982)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/custom-wp-query-help/)
 * 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/](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).'"]');
   
       }
       ```
   
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fcustom-wp-query-help%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 11 replies - 1 through 11 (of 11 total)

 *  Plugin Author [Darren Cooney](https://wordpress.org/support/users/dcooney/)
 * (@dcooney)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/custom-wp-query-help/#post-11561297)
 * Hi [@doctorlam1982](https://wordpress.org/support/users/doctorlam1982/) I’m not
   able to access the site because it’s password protected.
 * Strange issue though, how many post IDs are listed in the post—in days param?
 * Have you tried it without sticky posts enabled?
 *  Thread Starter [doctorlam1982](https://wordpress.org/support/users/doctorlam1982/)
 * (@doctorlam1982)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/custom-wp-query-help/#post-11561429)
 * Hi [@dcooney](https://wordpress.org/support/users/dcooney/) , I’ve unpassword
   protected it so you can take a look. I’ve tried without sticky posts and it didn’t
   help.
 * I”m not sure what you mean by how many post IDs listed in days param? I know 
   that it should load 8 posts but is always only loading 5.
 *  Plugin Author [Darren Cooney](https://wordpress.org/support/users/dcooney/)
 * (@dcooney)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/custom-wp-query-help/#post-11561535)
 * Ok no worries. I can look later tonight.
    Did you try turning off the sticky 
   posts shortcode parameter?
 *  Thread Starter [doctorlam1982](https://wordpress.org/support/users/doctorlam1982/)
 * (@doctorlam1982)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/custom-wp-query-help/#post-11561557)
 * Thank you! I tried that, it didn’t make any difference.
 *  Plugin Author [Darren Cooney](https://wordpress.org/support/users/dcooney/)
 * (@dcooney)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/custom-wp-query-help/#post-11562221)
 * Yes, so I inspected the HTML and it’s only pulling 5 posts.
    `data-post-in="1103,1101,737,735,733"`
 * Meaning ALM, is grabbing the correct posts.
 * Id start by debugging your custom query to see why it’s only pulling 5 posts.
 *  Thread Starter [doctorlam1982](https://wordpress.org/support/users/doctorlam1982/)
 * (@doctorlam1982)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/custom-wp-query-help/#post-11562235)
 * Thanks for checking! Based on the query I posted above, do you see anything glaring?
   I can’t figure it out for the life of me.
 *  Plugin Author [Darren Cooney](https://wordpress.org/support/users/dcooney/)
 * (@dcooney)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/custom-wp-query-help/#post-11562242)
 * Actually, the query looks wrong.
 * try this.
 *     ```
       <?php
       // query
       $related_blogs = get_posts(
          array(
             'post_type' => 'post',
             'posts_per_page' => -1, // get all posts
             'post__not_in' => array($post->ID),
             'tax_query' => array(
                array(
                   'taxonomy' => 'category',
                   'field' => 'id',
                   'terms' => $term_ids,      
                   'operator'=> 'IN' //Or 'AND' or 'NOT IN'
                )
             )
          )
       );
       ```
   
 * You had posts_per_page in the wrong spot
    -  This reply was modified 6 years, 11 months ago by [Darren Cooney](https://wordpress.org/support/users/dcooney/).
 *  Thread Starter [doctorlam1982](https://wordpress.org/support/users/doctorlam1982/)
 * (@doctorlam1982)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/custom-wp-query-help/#post-11562342)
 * Amazing! Thank you! That worked!
 *  Plugin Author [Darren Cooney](https://wordpress.org/support/users/dcooney/)
 * (@dcooney)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/custom-wp-query-help/#post-11562674)
 * Awesome, no problem at all 🙂
    Please consider a review if you have some time.
   [https://wordpress.org/support/plugin/ajax-load-more/reviews/](https://wordpress.org/support/plugin/ajax-load-more/reviews/)
 *  Thread Starter [doctorlam1982](https://wordpress.org/support/users/doctorlam1982/)
 * (@doctorlam1982)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/custom-wp-query-help/#post-11570333)
 * I will definitely write a review! I have one more question. The query works perfectly
   but when there are no matching categories, all the posts get loaded. Is there
   something I’m missing thst is causing that?
 *  Plugin Author [Darren Cooney](https://wordpress.org/support/users/dcooney/)
 * (@dcooney)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/custom-wp-query-help/#post-11571900)
 * You would have to hide ALM if no posts match.
 *     ```
       if(!empty($post_in)){
       	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).'"]');
       }
       ```
   
 * Hope that helps.

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Custom WP Query Help’ is closed to new replies.

 * ![](https://ps.w.org/ajax-load-more/assets/icon-256x256.png?rev=2944639)
 * [Ajax Load More – Infinite Scroll, Load More, & Lazy Load](https://wordpress.org/plugins/ajax-load-more/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/ajax-load-more/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/ajax-load-more/)
 * [Active Topics](https://wordpress.org/support/plugin/ajax-load-more/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/ajax-load-more/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/ajax-load-more/reviews/)

## Tags

 * [custom-query](https://wordpress.org/support/topic-tag/custom-query/)
 * [limit](https://wordpress.org/support/topic-tag/limit/)

 * 11 replies
 * 2 participants
 * Last reply from: [Darren Cooney](https://wordpress.org/support/users/dcooney/)
 * Last activity: [6 years, 11 months ago](https://wordpress.org/support/topic/custom-wp-query-help/#post-11571900)
 * Status: resolved