I have two Loops using WP_Query.
The first loop shows X posts. No pagination.
(loop A)
The second loop shows Y posts. With custom pagination.
(loop B)
It kinda works; but you can see the second loop repeating X posts from the first loop. First thing that comes to mind is to set an offset (equal to X), but it breaks pagination and there is a whole page on Codex dedicated to this issue. It also has a workaround, but the problem is I have two loops so this workaround applies to both of them.
So the question is
How do I make this work as intended?
A possible solution would be making the code from Codex "target" the second Loop, but I'm not a programmer and I don't know how to do this. Maybe there is even a better way to achieve what I'm trying to do, but I can't think of any other options.
Additional info
1) I can't use only one Loop because of how complicated my markup is.
2) All the code was found on the Net
3) Both loops are not restricted by authors or categories or whatever. It is always the same content with two different layouts "combined" to look like single "feed".
and since I can't post more than 2 links, here is the pagination code
function custom_pagination($numpages = '', $pagerange = '', $paged='') {
if (empty($pagerange)) {
$pagerange = 2;
}
/**
This first part of our function is a fallback
for custom pagination inside a regular loop that
uses the global $paged and global $wp_query variables.
It's good because we can now override default pagination
in our theme, and use this function in default quries
and custom queries.
**/
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
/**
We construct the pagination arguments to enter into our paginate_links
function.
**/
$pagination_args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'total' => $numpages,
'current' => $paged,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<nav class='custom-pagination'>";
echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
echo $paginate_links;
echo "</nav>";
}
}
solution from Codex
add_action('pre_get_posts', 'myprefix_query_offset', 1 );
function myprefix_query_offset(&$query) {
//Before anything else, make sure this is the right query...
if ( ! $query->is_home() ) {
return;
}
//First, define your desired offset...
$offset = 1;
//Next, determine how many posts per page you want (we'll use WordPress's settings)
$ppp = get_option('posts_per_page');
//Next, detect and handle pagination...
if ( $query->is_paged ) {
//Manually determine page query offset (offset + current page (minus one) x posts per page)
$page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
//Apply adjust page offset
$query->set('offset', $page_offset );
}
else {
//This is the first page. Just use the offset...
$query->set('offset',$offset);
}
}
add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );
function myprefix_adjust_offset_pagination($found_posts, $query) {
//Define our offset again...
$offset = 1;
//Ensure we're modifying the right query object...
if ( $query->is_home() ) {
//Reduce WordPress's found_posts count by the offset...
return $found_posts - $offset;
}
return $found_posts;
}