Offset via pre_get_posts affects other loops and repeater fields.
-
I have a static front page at https://veggiemagnifique.com – a client’s blog. This shows the 5 most recent posts via two loops, one displaying the entire first post and the next displaying excerpts from posts 2-4. At the bottom of the page is a link to https://veggiemagnifique.com/blog/ labelled “load more”.
I have been using an offset via pre_get_posts in the functions.php file of the theme to preserve pagination on this posts page (/blog) and begin it at the 6th most recent post, but have noticed that for some reason this affects all the repeater fields I have created in the CMS (via the ACF plugin), including in single posts and on the homepage. I presume that my code (below) is not targeted enough to only affect the feed on the posts page but no combination of is_front_page, is_home, is_posts_page or is_page($ID) has narrowed it down.
I created this from these guidelines: https://codex.www.ads-software.com/Making_Custom_Queries_using_Offset_and_Pagination
add_action('pre_get_posts', 'myprefix_query_offset', 1 ); function myprefix_query_offset(&$query) { if ($query->is_front_page() && $query->is_home()) { 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); } } add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 ); function myprefix_adjust_offset_pagination($found_posts, $query) { $offset = 5; if ($query->is_front_page() && $query->is_home()) { return $found_posts - $offset; } return $found_posts; }
I currently have this function commented out in my functions.php file so that the site works properly, so we are living with the lack of offset at the blog page. ACF have stated that this is a “known issue” and that “an alternative” to pre_get_posts should be used, but I have not found one which preserves pagination and doesn’t affect other loops around the site.
Does anyone have a solution? Thank you.
- The topic ‘Offset via pre_get_posts affects other loops and repeater fields.’ is closed to new replies.