2

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
    Do you mean that on page 2 etc you're getting the same results as on page 1? Could you include your WP_Query in your question please? – James Barrett Jun 20 '15 at 14:12
  • As you have it written your "index" is your main query, and your "main query" is a secondary query. Can you post the code in more context. – s_ha_dum Jun 20 '15 at 15:08
  • Yes exactly - on page 2 Im getting the same results as on page 1. Its like infinite loop, if I keep scrolling I will get 100 pages with the same content. About the second comment, you are correct too. Loop.php contains my WP_Query and my index is just a wrapper. I dont mind to post more code but what exactly you have in mind ? – tslid Jun 20 '15 at 15:55
  • There is no pagination apparent in your code. Can you please post more context? – s_ha_dum Jun 20 '15 at 16:54
  • Post your index.php and loop.php. To be honest, I have difficulty to understand your code. From your previous question, I do feel pre_get_posts is a better option – Pieter Goosen Jun 20 '15 at 17:11
2

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

| improve this answer | |
  • This is working okay for the first page but as soon as the second one loads it's showing posts without thumbnails. I have updated the first post with full code of my loop and index.php files. Once again thanks for your time - the issue may not be fully solved but Im learning a lot. – tslid Jun 20 '15 at 20:54
  • Are you using infinite scrolling. If so, this is probably your issue and you should post your code for infinite scrolling then. Apart from that, I really see no other issues that can cause this issue – Pieter Goosen Jun 21 '15 at 6:00
  • When there are no more posts my infinite scroll is supposed to break, with my original code when I reach the end of the posts I see No More Posts - I guess thats the moment when I go to $has_posts = false; For the infinite scroll I found only this if(is_home() and blt_get_option('enable_infinite_scrolling')){ $output[] = 'infinite-scroll'; } – tslid Jun 21 '15 at 9:21
  • Infinite scroll (usually) use a custom query to return the next set of posts. This custom query is constructed exactly from the main query. When the main query is altered in the way we (correctly) did, it breaks the infinite scroll functionality. You have to manually adjust that custom query used by infinite scroll to reflect the changes in the main query. You will have to find where your infinite scrolling is defined so that this function can be adjusted. Infinite scrolling a real pain when the main query is altered or in case of custom queries. – Pieter Goosen Jun 21 '15 at 9:48
  • Got it. I found where is located but on first sight it it is not creating custom query, it only calling the loop file. I may be wrong but... I posted the code in the first post. – tslid Jun 21 '15 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.