Support

Account

Home Forums ACF PRO Repeater Fields and pre_get_posts Reply To: Repeater Fields and pre_get_posts

  • I have set up a test and I put just this in the functions.php file of the site

    
    
    	
    	
    	add_action('pre_get_posts', 'myprefix_query_offset', 1);
    	function myprefix_query_offset(&$query) {
    	
    		if (is_admin() || !$query->is_home() || !$query->is_main_query()) {
    			return;
    		}
    		$offset = 5;
    		$ppp = get_option('posts_per_page');
    
    		if ($query->is_paged ) {
    			$page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
    			$query->set('offset', $page_offset );
    		}
    		else {
    			$query->set('offset',$offset);
    		}
    	}
    

    This seems to offset the posts shown on the blog page by 5 without effecting the repeater. You can see this test here http://www.ssirdc.com.php56-18.dfw3-2.websitetestlink.com/blog-page. The first post on this page is the post that’s marked as #6. I added a repeater to this post that has 10 rows and you can see that all 10 rows are being shown. I also set the number of posts to show on a page to 16 to match yours. If you go to the second page you’ll see that it starts with post 22 (5 offset + 16 on the first page). I also added rows to the repeater and it is not effected here either.

    I’m not exactly sure what your second filter does or why it is needed

    
    
    add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );
    function myprefix_adjust_offset_pagination($found_posts, $query) {
        $offset = 5;
         if ($query->is_home()) {
            return $found_posts - $offset;
        }
        return $found_posts;
    }