Title: Infinite scroll &#8211; help with offset when using meta_query parameter
Last modified: December 19, 2016

---

# Infinite scroll – help with offset when using meta_query parameter

 *  [Andrew](https://wordpress.org/support/users/snd26/)
 * (@snd26)
 * [9 years, 6 months ago](https://wordpress.org/support/topic/infinite-scroll-help-with-offset-when-using-meta_query-parameter/)
 * Hello, thanks for all the work done on this great plugin. I just have a question
   about getting some filtering working with the infinite scroll module.
 * I used to have a problem using the `orderby` parameter to order posts by `comment_count`
   and my custom post metas – `post_rating`, `post_views`. The problem was the starting
   position (offset) was wrong. It causes a double load first posts instead of carrying
   on from the last post of the custom WordPress loop. This was a bug that I recently
   got fixed by looking up here:
    [https://github.com/Automattic/jetpack/issues/1135](https://github.com/Automattic/jetpack/issues/1135)
   I tried all those solutions and they all work. I’ve got the high priority of 
   the `infinite_scroll_query_args filter` and set an offset: `$args['offset'] =
   $args['posts_per_page'] * $args['paged'];`
 * I’ve got infinite scroll working with all parameters – `type`, `category`, `taxonomy`,`
   orderby`. But I’ve got one last problem when using the `meta_query`, it’s similar
   to the orderby bug. For example, this code uses my rating_count post meta to 
   only show posts that have a rating of of at least 1:
 *     ```
       			$args['meta_query'] = array(
       				array(
       					'key' 		=> 'rating_count',
       					'value' 	=> 1,
       					'compare' 	=> '>=',
       				),
       			);
       ```
   
 * This meta query works with pagination as normal. But setting up this meta query
   for infinite scroll using the `infinite_scroll_query_args filter` – it works,
   except the starting position (offset) is incorrect. It’s like the problem I had
   with the orderby parameter, but this one is a bit more difficult to fix because
   the offset starts at a different place depending on how many posts are found.
   Any ideas of how I can fix this? I think I need a calculation on the offset parameter
   but I can’t work out what the calculation needs to be.
 * Any help appreciated.
    -  This topic was modified 9 years, 6 months ago by [Andrew](https://wordpress.org/support/users/snd26/).
    -  This topic was modified 9 years, 6 months ago by [Andrew](https://wordpress.org/support/users/snd26/).
    -  This topic was modified 9 years, 6 months ago by [Andrew](https://wordpress.org/support/users/snd26/).
    -  This topic was modified 9 years, 6 months ago by [Andrew](https://wordpress.org/support/users/snd26/).
    -  This topic was modified 9 years, 6 months ago by [Andrew](https://wordpress.org/support/users/snd26/).
    -  This topic was modified 9 years, 6 months ago by [Andrew](https://wordpress.org/support/users/snd26/).
    -  This topic was modified 9 years, 6 months ago by [Andrew](https://wordpress.org/support/users/snd26/).
    -  This topic was modified 9 years, 6 months ago by [Andrew](https://wordpress.org/support/users/snd26/).
    -  This topic was modified 9 years, 6 months ago by [Andrew](https://wordpress.org/support/users/snd26/).
    -  This topic was modified 9 years, 6 months ago by [Andrew](https://wordpress.org/support/users/snd26/).
    -  This topic was modified 9 years, 6 months ago by [Andrew](https://wordpress.org/support/users/snd26/).

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

 *  Plugin Author [Jeremy Herve](https://wordpress.org/support/users/jeherve/)
 * (@jeherve)
 * Jetpack Mechanic 🚀
 * [9 years, 6 months ago](https://wordpress.org/support/topic/infinite-scroll-help-with-offset-when-using-meta_query-parameter/#post-8570645)
 * To be honest you’re pushing the Infinite Scroll module to its limits here 🙂 
   This may be outside of the realm of things you can do with Infinite Scroll.
 * > the offset starts at a different place depending on how many posts are found
 * Could you try to take this into account and change the argument accordingly, 
   when defining the `offset` value for `infinite_scroll_query_args`?
 *  Thread Starter [Andrew](https://wordpress.org/support/users/snd26/)
 * (@snd26)
 * [9 years, 6 months ago](https://wordpress.org/support/topic/infinite-scroll-help-with-offset-when-using-meta_query-parameter/#post-8573181)
 * I’ve tried different calculations for the `offset` using found posts in the calculation,
   but I can’t work it out.
 * I use the WordPress custom loop to it’s fullest using `$the_query = new WP_Query(
   $loop_args );`
    So I will try and push infinite scroll to its limits 🙂 It is
   all working fine though except for that incorrect offset for my `meta_query` 
   rating range. The `date_query` (which is similar to the `meta_query` parameter)
   which shows posts between a date range is working with the correct offset.
 * I’m currently just using the `infinite_scroll_query_args` filter to get the correct
   offset for the rating range:
 *     ```
       // show events with at least a rating of 1
       function hp_jetpack_infinite_scroll_query_args( $args ) {
               $args['post_type'] = 'event';
       	$args['meta_query'] = array(
       	       array(
       			'key' 		=> 'rating_count',
       			'value' 	=> 1,
       			'compare' 	=> '>=',
       		)
       	);	
   
           return $args;
       }
   
       add_filter( 'infinite_scroll_query_args', 'hp_jetpack_infinite_scroll_query_args', 100 );
       ```
   
 * Once I get that rating range working correctly, my full infinite scroll filter
   looks like this:
 *     ```
       function hp_jetpack_infinite_scroll_query_args( $args ) {
   
       	// defaults
       	$args['post_type'] = 'event';
       	$args['orderby'] = 'date';
       	$args['order']   = 'DESC';
   
       	// User control loop Filters
       	if( hp_is_url_query_string() ) {
   
       		// Type (post type)
       		if( hp_query_string( 'type' ) ) {
       			$args['post_type'] = hp_get_sanitized_url_query_string_type_value();
       		}
   
       		// Search term
       		if( hp_query_string( 's' ) ) {
       			$args['s'] = get_search_query();
       		}
   
       		// Event category (taxonomy)
       		if( hp_query_string( 'event_category' ) ) {
       			$args['event_category'] = hp_get_sanitized_url_query_string_event_category_value();
       		}
   
       		// Order by date posted
       		if( hp_query_string( 'orderby', 'newest' ) ) {	
       			$args['orderby'] = 'date';
       			$args['order']   = 'DESC';
   
       			// bug fix
       			$paged = (int)$_POST['page'] + 1;
       			$args['paged'] = $paged;
       		}
   
       		// Order by comment count
       		if( hp_query_string( 'orderby', 'comment_count' ) ) {	
       			$args['orderby'] = 'comment_count';
       			$args['order']   = 'DESC';
   
       			// bug fix
       			$paged = (int)$_POST['page'] + 1;
       			$args['paged'] = $paged;
       		}
   
       		// Order by Rating
       		if( hp_query_string( 'orderby', 'rating' ) ) {	
       			$args['meta_key'] = 'rating_count';
       			$args['orderby'] = 'meta_value_num';
       			$args['order']   = 'DESC';
   
       			// bug fix
       			$paged = (int)$_POST['page'] + 1;
       			$args['paged'] = $paged;
       		}
   
       		// Order by view count
       		if( hp_query_string( 'orderby', 'view_count' ) ) {	
       			$args['meta_key'] = 'view_count';
       			$args['orderby'] = 'meta_value_num';
       			$args['order']   = 'DESC';
   
       			// bug fix
       			$paged = (int)$_POST['page'] + 1;
       			$args['paged'] = $paged;
       		}
   
       		// Date range
       		if( hp_query_string( 'after' ) || hp_query_string( 'before' ) ) {
       			$args['date_query'] = array(
       				array(
       					'after'     => hp_get_sanitized_url_query_string_after_value(),
       					'before'	=> hp_get_sanitized_url_query_string_before_value(),
       				)
       			);
       		}
   
       		// rating range - Bug, incorrect offset
       		if( hp_query_string( 'rating' ) ) {
       			$args['meta_query'] = array(
       				array(
       					'key' 		=> 'rating_count',
       					'value' 	=> hp_get_sanitized_url_query_string_rating_value(),
       					'compare' 	=> '>=',
       				)
       			);
       		}		
       	}
   
           return $args;
       }
       add_filter( 'infinite_scroll_query_args', 'hp_jetpack_infinite_scroll_query_args', 100 );
       ```
   
 * I now know there is at least 2 things that is affecting the offset of my `meta_query`
   rating range:
 * 1. the `posts_per_page` set in admin. If I set this to 1 post per page, there
   will be no offset which means I can use the same bug fix used on the orderby 
   parameter.
 * 2. The other thing affecting the offset is the total posts of the post type (
   not the found posts).
    So if the `wp_count_posts( 'your_post_type' )->publish`
   is much higher than the: `$wp-query->found_posts;` There will be no offset, which
   means I can add the same bug fix used on the `orderby` parameter.
 * I don’t know if you can work out a fix for this based on this information.
    -  This reply was modified 9 years, 6 months ago by [Andrew](https://wordpress.org/support/users/snd26/).
 *  Plugin Author [Jeremy Herve](https://wordpress.org/support/users/jeherve/)
 * (@jeherve)
 * Jetpack Mechanic 🚀
 * [9 years, 6 months ago](https://wordpress.org/support/topic/infinite-scroll-help-with-offset-when-using-meta_query-parameter/#post-8573518)
 * > I’ve tried different calculations for the `offset` using found posts in the
   > calculation, but I can’t work it out.
 * That seems to be the key here. However I’m afraid that’s a bit outside of my 
   wheelhouse. While I’m happy to help with basic troubleshooting with Jetpack’s
   functions and code, I’m unable to provide help with customizations of this nature.
 * At this point, it might be worth reaching out to someone more knowledge about`
   WP_Query` than me. You could post in [the Hacks subforum](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
   for example, or look for a pro [here](https://jetpack.pro/).
 * Sorry not to be able to help you more!

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

The topic ‘Infinite scroll – help with offset when using meta_query parameter’ is
closed to new replies.

 * ![](https://ps.w.org/jetpack/assets/icon.svg?rev=2819237)
 * [Jetpack - WP Security, Backup, Speed, & Growth](https://wordpress.org/plugins/jetpack/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/jetpack/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/jetpack/)
 * [Active Topics](https://wordpress.org/support/plugin/jetpack/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/jetpack/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/jetpack/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [Jeremy Herve](https://wordpress.org/support/users/jeherve/)
 * Last activity: [9 years, 6 months ago](https://wordpress.org/support/topic/infinite-scroll-help-with-offset-when-using-meta_query-parameter/#post-8573518)
 * Status: not resolved