WordPress.org

Ready to get started?Download WordPress

Forums

Jetpack by WordPress.com
[resolved] Duplicate posts when using Infinite loop and offset (23 posts)

  1. 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/

  2. Jeremy Herve
    Happiness Engineer
    Plugin Author

    Posted 1 year ago #

    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!

  3. 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

  4. kkalbat
    Member
    Posted 8 months ago #

    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.

  5. Jeremy Herve
    Happiness Engineer
    Plugin Author

    Posted 8 months ago #

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

  6. kkalbat
    Member
    Posted 8 months ago #

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

  7. maxgx
    Member
    Posted 5 months ago #

    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?

  8. Jeremy Herve
    Happiness Engineer
    Plugin Author

    Posted 5 months ago #

    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.

  9. maxgx
    Member
    Posted 5 months ago #

    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?

  10. Jeremy Herve
    Happiness Engineer
    Plugin Author

    Posted 5 months ago #

    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

  11. maxgx
    Member
    Posted 5 months ago #

    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!

  12. maxgx
    Member
    Posted 5 months ago #

    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...

  13. Jeremy Herve
    Happiness Engineer
    Plugin Author

    Posted 5 months ago #

    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

  14. maxgx
    Member
    Posted 5 months ago #

    $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"

  15. Jeremy Herve
    Happiness Engineer
    Plugin Author

    Posted 5 months ago #

    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.

  16. maxgx
    Member
    Posted 5 months ago #

    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' );
  17. Jeremy Herve
    Happiness Engineer
    Plugin Author

    Posted 5 months ago #

    Thanks for bearing with me.

    I'll ask one of our Infinite Scroll experts to take a look. We'll post again here as soon as we have news.

  18. maxgx
    Member
    Posted 5 months ago #

    no no, thank you for your support!

  19. maxgx
    Member
    Posted 4 months ago #

    hi Jeremy, any news from the experts yet?

  20. Jeremy Herve
    Happiness Engineer
    Plugin Author

    Posted 4 months ago #

    Not yet, but I'll post again here as soon as they get back to me!

  21. Jeremy Herve
    Happiness Engineer
    Plugin Author

    Posted 4 months ago #

    It seems I made a mistake in the code snippet I posted a few weeks ago. Could you try to use the infinite_scroll_query_args filter instead of infinite_scroll_allowed_vars?

    // Add post__not_in to IS query args
    function jeherve_is_allowed_vars( $vars ) {
    	$vars[] = 'post__not_in';
    	return $vars;
    }
    add_filter( 'infinite_scroll_query_args', 'jeherve_is_allowed_vars' );

    Let me know if it helps.

  22. maxgx
    Member
    Posted 4 months ago #

    hey Jeremy, it does work!
    of course this only hides sticky posts, but the duplication of posts when 'older posts' is clicked is still there.
    is there any way to let IS count/consider a custom query rather than the standard WP loop?

  23. Jeremy Herve
    Happiness Engineer
    Plugin Author

    Posted 4 months ago #

    Could you send me an email with all the code you've used to build this custom loop? I will then check it on my own test site and see how I can help.
    http://jetpack.me/contact-support/

    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic