• Resolved doctorlam1982

    (@doctorlam1982)


    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).'"]');
    
    }

    The page I need help with: [log in to see the link]

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Darren Cooney

    (@dcooney)

    Hi @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

    (@doctorlam1982)

    Hi @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

    (@dcooney)

    Ok no worries. I can look later tonight.
    Did you try turning off the sticky posts shortcode parameter?

    Thread Starter doctorlam1982

    (@doctorlam1982)

    Thank you! I tried that, it didn’t make any difference.

    Plugin Author Darren Cooney

    (@dcooney)

    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

    (@doctorlam1982)

    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

    (@dcooney)

    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.
    Thread Starter doctorlam1982

    (@doctorlam1982)

    Amazing! Thank you! That worked!

    Plugin Author Darren Cooney

    (@dcooney)

    Awesome, no problem at all 🙂
    Please consider a review if you have some time.
    https://wordpress.org/support/plugin/ajax-load-more/reviews/

    Thread Starter doctorlam1982

    (@doctorlam1982)

    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

    (@dcooney)

    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.