• I am trying to use infinity scroll and for some reason it is repeating the initial set of posts and when I reach the end it is jumping back up and repeating the same thing again, causing an erratic scrolling behaviour. I have a custom query which is ordering the posts by a field (added by ACF) called event_date.

    I am adding infinity scroll support to my theme as follows:

    function infinite_scroll_init() {
    	add_theme_support( 'infinite-scroll', array(
    			'container' => 'events',
    			'footer' => false,
    			'footer_widgets' => false,
    			'posts_per_page' => get_option('posts_per_page'),
    			'render' => 'infinite_scroll_render',
    	) );
    }
    add_action( 'after_setup_theme', 'infinite_scroll_init');
    
    function infinite_scroll_render() {
    	get_template_part( 'content','posts' );
    }
    
    function jetpack_infinite_scroll_query_args( $args ) {
    	$args['order']   = 'DESC';
    	$args['meta_key'] = 'event_date';
            $args['orderby'] = 'meta_value';
    	return $args;
    }
    add_filter( 'infinite_scroll_query_args', 'jetpack_infinite_scroll_query_args' );

    My content-posts.php looks like this (it is also being included from index.php with get_template_part().

    $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
    $posts_per_page = (isset($posts_per_page)) ? $posts_per_page : get_option('posts_per_page');
    
    $args = array(
        'post_type' => 'post',
        'meta_key' => 'event_date',
        'orderby' => 'meta_value date',
        'order' => 'DESC',
        'posts_per_page' => $posts_per_page,
        'paged' => $paged
    );
    
    $date_format = "jS F Y";
    
    $query = new WP_Query( $args );
    if ($query->have_posts()) : ?>
       <?php  $index = 0; ?>
        <?php while ( $query->have_posts() ) : $query->the_post(); ?>
    
         <?php $event_date = get_field('event_date');
            if ($event_date) {
                $event_date = date_i18n($date_format, strtotime($event_date));
            } else { //fallback which shouldn't really happen, event_date is compulsory
                $event_date = get_the_date($date_format);
            } ?>
    
           <?php $thumbnail_url = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'large' )[0]; ?>
            <a class="news" style="background:url(<?php echo $thumbnail_url; ?>) no-repeat center center;" href="<?php the_permalink(); ?>">
                <span class="caption"><h1><?php the_title(); ?></h1><h2>- <?php echo $event_date; ?> -</h2></span>
            </a>
    
            <?php if ($index % 3 == 3) : ?></div><?php endif; ?>
            <?php $index++; ?>
          <?php endwhile; // end of the loop. ?>
    
          <?php if ($index % 3 != 3) : ?></div><?php endif; ?>
    
    <?php endif; ?>
    <?php wp_reset_query(); ?>

    It seems like it has something to do with the $paged variable, but can’t figure how to go around this. What can I do to get this to work properly?

    https://ww.wp.xz.cn/plugins/jetpack/

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

    (@jeherve)

    Jetpack Mechanic 🚀

    I don’t think the way you use orderby will work:

    'orderby' => 'meta_value date',

    Could you try the solution recommended here?
    http://wordpress.stackexchange.com/a/15499/27574

    If that still doesn’t work, it might be worth trying to remove posts_per_page and paged from the equation, and see if that changes anything.

    Let me know how it goes.

    Thread Starter jbx

    (@jbx)

    Not sure what exactly that solution is suggesting. What should I do use the old way instead of the new WP_Query? Other than that it doesn’t seem to do anything.

    I don’t actually need to have that orderby with 2 fields, so I removed the second one, but still same effect. Infinity scroll just gets confused.

    Could you explain a bit in detail what is infinity scroll doing and how it is working underneath the hood? Seems to be doing something weird with pagination, because suddenly I see /page/2 but its not clear what is actually happening, and there is nothing in the docs that explains this and how you should design your custom index pages to work well with it.

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    Could you contact us through this contact form and mention this thread? I’d like to run some more tests with you, but I’d need to know more about your theme setup.

    Thanks!

    Could you explain a bit in detail what is infinity scroll doing and how it is working underneath the hood?

    You can check the source code here to find out more:
    https://github.com/Automattic/jetpack/tree/3.8.1/modules/infinite-scroll

    Seems to be doing something weird with pagination, because suddenly I see /page/2 but its not clear what is actually happening

    We use the HTML5 History API to modify the URL without actually reloading the page. It allows your readers to be able to refer to the right page when sharing a link to your site.
    https://github.com/Automattic/jetpack/blob/3.8.1/modules/infinite-scroll/infinity.php#L785

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Infinity scroll repeating initial posts’ is closed to new replies.