WordPress.org

Support

Support » Plugins and Hacks » Jetpack by WordPress.com » [Resolved] Duplicate posts when using Infinite loop and offset

[Resolved] Duplicate posts when using Infinite loop and offset

  • Christine Rondeau

    @crondeau

    Volunteer Forum Moderator

    I was trying to get jetpack’s infinite loop on my site and it’s working just fine, but of course the design specified that the first 2 posts have a slightly different design, so I thought I could just use a wp_query like so:

    <?php
            $loop = new WP_Query( array(
              'ignore_sticky_posts'  => true,
              'offset' => 2,
            ));
          while ( $loop->have_posts() ) : $loop->the_post(); 
    
              get_template_part( 'content' );
    
        endwhile;  wp_reset_query(); ?>

    That’s all great and infinite loop works, but the next 2 posts are repeats of the last two. I found this thread – http://wordpress.org/support/topic/custom-queries-and-how-to-avoid-jetpack-infinite-scroll-to-duplicate-post?replies=5
    and thought that it might be related, but since it’s 5 months old, I thought I would check.

    Is jetpack’s status still the same? If not any idea how this could be achieved? I tried using a counter as well instead of offset, but no joy…

    PS: I love jetpack by the way… 🙂

    http://wordpress.org/plugins/jetpack/

Viewing 15 replies - 1 through 15 (of 22 total)
  • Plugin Author Jeremy Herve

    @jeherve

    I’m afraid we haven’t found any solution to this problem yet. We’ve been working on improving Infinite Scroll to change how we capture arguments from the main query, but we still need to work on this.

    I’ll post again here as soon as we have a solution!

    Christine Rondeau

    @crondeau

    Volunteer Forum Moderator

    Glad i checked in with you instead of trying to get this to work.
    I think that we’ll just keep the design simple for now.

    Thanks Jeremy

    The question is: Is there a solid plan to implement wp_query for infinite scrolling, or should we just try and implement by ourselves?

    It would also be nice to post some updates on what features are being worked on right now and when they are expected to be rolled out.

    Plugin Author Jeremy Herve

    @jeherve

    @kkalbat I replied to your question here.

    It would also be nice to post some updates on what features are being worked on right now and when they are expected to be rolled out.

    If you’re interested in the things we’re working on at the moment, and if you want to help us test all the new features, you might be a good candidate for our Beta Group! Send us an email!

    You can also follow our progress by looking at opened tickets here. You can also contribute and submit patches there.

    The development log is also a good way to keep an eye on what’s happening with Jetpack.

    I hope this helps.

    Thank Jeremy, I would love to join the group. I will send an email soon.

    hi Jeremy
    with regard to your statement “Infinite Scroll now supports a greater range of queries”, is it possible for you to suggest how we can make use of IS support of queries to implement a solution for the questions above?

    i too have a custom query for a custom loop to exclude certain posts and also interrupt and start the loop further down the page using offset to show the rest, but when IS kicks in, it’s showing the same posts shown above.

    also, on the same thread g10r3 mentions a “featured content” module in jetpack which could be used to achieve this: where exactly is that? i cannot see it…

    any chance to get some hints?

    Plugin Author Jeremy Herve

    @jeherve

    is it possible for you to suggest how we can make use of IS support of queries to implement a solution for the questions above?

    We’ve made some changes to IS in r777844-plugins. That changeset introduces 2 new filters, infinite_scroll_allowed_vars and infinite_scroll_query_args.

    You can use create functions to add the new arguments and variables you use in your custom query, and hook them into the 2 filters, using a priority higher than the default (10), so that the filters get applied after our functions inject_query_args() and allowed_query_vars.

    I hope this helps.

    thanks a lot for the reply Jeremy! … although i’ve been banging my head against this brick-wall since…
    i’m not that strong in coding, so i’m finding difficult to implement what’s needed. i wonder if i can abuse your kindness some more.

    this is my first custom query:

    <?php
    $custom_args=array(
    	'posts_per_page'         => '2'
    ); // END $args
    $custom_query = null; // clear the query variable
    $custom_query = new WP_Query($custom_args);
     ?>
    		<?php if ( $custom_query->have_posts() ) : ?>
    			<?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
    				<?php get_template_part( 'content', get_post_format() ); ?>
    			<?php endwhile; ?>
    		<?php else : ?>
    			... something else ...
    		<?php endif; // end have_posts() check ?>
    		<?php wp_reset_query(); ?>

    after that, i have a sidebar call, then i start again with another loop, offsetting it from 2:

    <?php
    $custom2_args=array(
    	'offset'                 => '2'
    ); // END $args
    $custom2_query = null; // clear the query variable
    $custom2_query = new WP_Query($custom2_args);
     ?>
    		<?php if ( $custom2_query->have_posts() ) : ?>
    			<?php while ( $custom2_query->have_posts() ) : $custom2_query->the_post(); ?>
    				<?php get_template_part( 'content', get_post_format() ); ?>
    			<?php endwhile; ?>
    		<?php else : ?>
    			... something else ...
    		<?php endif; // end have_posts() check ?>
    		<?php wp_reset_query(); ?>

    should i move the lot into functions in functions.php? and how do i hook into the IS filters?

    Plugin Author Jeremy Herve

    @jeherve

    Could you try to use something like this instead, to avoid making changes to your loop?
    http://wordpress.org/support/topic/insert-sidebars-between-posts-how-do-i-code-this-in-my-indexphp?replies=3#post-3550028

    fantastic! that’s what i was looking for!
    and for hiding posts from IS i’m setting them as sticky, that’s working great too!
    thanks a lot Jeremy!

    damn! too quick to sing victory…

    i’m trying to exclude sticky posts from the main loop.

    if i use a custom query to exclude stickies, IS does not recognise it. clicking on “older posts” (by the way, this code seems to have no effect on the wordings) shows a list of posts starting from the first (with sticky post excluded) as per standard loop.

    i thought of using a function to manipulate the query for the main loop, intercepting it earlier (maybe?):

    function nosticky_home_loop( $query ) {
    	if ( $query->is_home() && $query->is_main_query() ) {
    		$query->set( 'post__not_in', get_option( 'sticky_posts' ) );
    	}
    }
    add_action( 'pre_get_posts', 'nosticky_home_loop' );

    but even this is not recognised by IS…

    Plugin Author Jeremy Herve

    @jeherve

    by the way, this code seems to have no effect on the wordings

    Try adding is_front_page to the list of conditionals.

    i thought of using a function to manipulate the query for the main loop, intercepting it earlier (maybe?):

    How about something like this?

    https://gist.github.com/jeherve/9739690

    $query->set( 'ignore_sticky_posts', true );
    only sets the query to treat sticky in the same way as other posts in the main loop.
    the problem with duplicating content seems to stay though: when i hit the “older posts” link, it starts with the first non-sticky post from the main loop again.

    replacing it with
    $query->set( 'post__not_in', get_option( 'sticky_posts' ) );
    removes them from the loop, but has the duplicate problem too.

    it seems that IS doesn’t like whenever/however the standard query is “retouched”

    Plugin Author Jeremy Herve

    @jeherve

    hm. That’s annoying. Could you try to add post__not_in to the list of allowed query args, like so:

    https://gist.github.com/jeherve/2541d2eef8231507d299

    Let me know if it helps.

    no, that doesn’t seem to work either.

    these are the functions now, in that order (i tried reversing):

    // Add post__not_in to IS query args
    function jeherve_is_allowed_vars( $vars ) {
    	$vars[] = 'post__not_in';
    	return $vars;
    }
    add_filter( 'infinite_scroll_allowed_vars', 'jeherve_is_allowed_vars' );
    
    // Customize the loop on home page only
    function jeherve_custom_home_query( $query ) {
    	if ( ! $query->is_main_query() )
    		return;
    	if ( ! $query->is_home() || ! $query->is_front_page() || is_admin() )
    		return;
    	$query->set( 'post__not_in', get_option( 'sticky_posts' ) );
    }
    add_filter( 'pre_get_posts', 'jeherve_custom_home_query' );
Viewing 15 replies - 1 through 15 (of 22 total)
  • The topic ‘[Resolved] Duplicate posts when using Infinite loop and offset’ is closed to new replies.
Skip to toolbar