I’m showing on my front page only posts with featured image. My problem is that on every next page load I receive the same results aka getting duplicates. I’m using ordering by custom field. Any directions/ideas what may lead to this problem are welcome. Below is my main loop

// Layout
if(is_category())
{
    $post_layout = blt_get_option('category_post_layout', 'normal');
}
elseif(is_author())
{
    $post_layout = blt_get_option('author_post_layout', 'normal');
}
elseif(is_archive())
{
    $post_layout = blt_get_option('archive_post_layout', 'normal');
}
else
{
    $post_layout = blt_get_option('home_post_layout', 'normal');
}


$i = 1;
if(have_posts()){ 

    while(have_posts()){ 

        the_post();
        get_template_part( 'inc/template-parts/content', $post_layout );


        // Ad spot #4
        if($ad_spot_between_posts = blt_get_option('spot_between_posts', 'none') != 'none'){

            $ad_posts_frequency = blt_get_option('spot_between_posts_frequency', 3);

            // take into account ad frequency
            if(($i % (int) $ad_posts_frequency) == 0){
                blt_get_ad_spot( 'spot_between_posts' );
            }

        }
        $i++;

    }

}else{

    $has_posts = false;

}

And this is my index :

  <?php get_header(); ?>


<div id="site-content" class="clearfix">

    <?php

        // Front Page - Top of container
        if(is_front_page() and is_active_sidebar('front-top_of_container')){
            dynamic_sidebar('front-top_of_container');
        }

    ?>
    <div id="site-content-column"><?php

        if(is_archive()){
            blt_get_title();        
        }


        if(have_posts()){ 

            echo '<div class="row">';
            include(get_template_directory().'/loop.php');
            echo '</div>';


            // Previous/next page navigation.
            if(!blt_get_option('enable_infinite_scrolling')){

                the_posts_pagination(array(
                    'prev_text'          => '<i class="fa fa-chevron-left"></i>',
                    'next_text'          => '<i class="fa fa-chevron-right"></i>',
                    'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'blue' ) . ' </span>',
                ));

            }           

        }else{ 

            get_template_part( 'inc/template-parts/content', 'none' );

        } ?> 

    </div><?php

    #   
    #   SIDEBAR
    #   ========================================================================================
    #   Load the sidebar if needed
    #   ========================================================================================
    #       
    if(in_array(blt_get_option('sidebar_layout', 'right'), array('left', 'right'), true)){
        get_sidebar();
    } ?>

</div>

<?php get_footer(); ?>

Edit: Infinite scroll function

function blt_infinitepaginate(){ 

    $has_posts      = true;
    $type           = isset($_POST['type']) ? $_POST['type'] : false;
    $loopFile       = $_POST['loop_file'];
    $paged          = $_POST['page_no'];
    $posts_per_page = get_option('posts_per_page');
    $post_id        = isset($_POST['post_id']) ? $_POST['post_id'] : false;

    # Load the posts
    if($post_id){

        include(get_template_directory().'/loop-hottest.php');

    }else{

        query_posts( array( 'paged' => $paged+1, 'post_status' => 'publish', 'post__not_in' => get_option('sticky_posts') ) );
        include(get_template_directory().'/loop.php');

    }

    if(!$has_posts){
        return false;
    }

    exit;

}

function blt_change_main_loop( $query ) {

    if ( is_admin() || ! $query->is_main_query() )
        return;

    if ( is_home() and isset($_GET['page'])) {
        $query->set( 'posts_per_page', $_GET['page'] * get_option('posts_per_page') );
        return;
    }

}
add_action( 'pre_get_posts', 'blt_change_main_loop', 1 );


}

1 Answer
1

It is quite hard to properly answer your question with the amount of context in your question, but I am going to try to use the context from your previous question

Having a bit more context here on template, and if I read this correctly, this is on your index.php, I still believe and stand by my point that pre_get_posts is your answer. Tackle this problem as follow (only if this is not a static front page, because the following will not work)

  • Remove your custom query. Go back to your default setup where you get all posts normally

  • Next, use pre_get_posts to alter the main query on your homepage to only display posts with thumbnails. This should solve your issue with pagination

Add the following in your functions.php: (Requires at least PHP 5.3)

add_action( 'pre_get_posts', function ( $q ) 
{
    if (     $q->is_home() // Target the home page only
          && $q->is_main_query() // Target only the main query
    ) {
        $meta_query = array(
            array(
                'key' => '_thumbnail_id'
            )
        );
        $q->set( 'meta_query', $meta_query );
    }
});

If this does not solve your issue, then you really need to post more context.

EDIT

I’m not going to go into detail about query_posts and how bad it really really is, I have covered that in my previous answer to your previous question. As you most probably did not code that, I would contact the developer about this.

Anyway, to quick solve your issue with infinite scrolling, you need to add the following to your query_posts arguments

'meta_query' => array( array( 'key' => '_thumbnail_id' ) ),

Just a note, I would rather rewrite the complete infinite scrolling function with WP_Query as described in your previous question

Leave a Reply

Your email address will not be published. Required fields are marked *