I've been on this problem for a week now. I've searched through countless posts that just aren't answering the question. It seems that my wp_query breaks pagination. I've looked through a lot of the post on wp_query and pagination on here but with still no joy.
As far as I can see it's set up correctly. I have one instance where it will show all posts in one page, and in the other it repeats the content on each page. See the code below for a stripped down version with notes.
Can anyone see what the problem is?
<?php
/*
Let's asume that this is the search query in the address bar:
?s=media&trend=&industry=&d=2012-05-17|2012-11-17
(To iterate, it's saying search for keyword 'media'
posted between 17th May 2012 and 17th Novemer 2012)
It gives 13 results and shows all on the same page.
I have set show 10 posts per page so it should be cutting off
at first 10.
PAGINATION: On line 56 If I change the code to
$wp_query->max_num_pages, the pagination works but then
repeats the same 10 posts.
*/
// Setup pagination
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// Sort trend taxonomy
$tr = $_GET['trend'];
// Sort industry taxonomy
$ind = $_GET['industry'];
// Set date var
$d = $_GET['d'];
$args = array(
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => 10,
'trend' => $tr,
'industry' => $ind
);
// Date range function, if available
if( (isset($_GET['d'])) && ($_GET['d']!=='') ) {
function filter_where( $where = '' ) {
$date_range = $_GET['d'];
$date_range = explode("|", $date_range);
$that_date = $date_range[0];
$this_date = $date_range[1];
$where .= " AND post_date >= '".$that_date."' AND post_date <= '".$this_date."'";
return $where;
}
// Add the filter
add_filter( 'posts_where', 'filter_where' );
}
// setup the wp_query
$allsearch = new WP_Query($args);
// Remove the date filter where clause
if( (isset($_GET['d'])) && ($_GET['d']!=='') ) {
remove_filter( 'posts_where', 'filter_where' );
}
while ( $allsearch->have_posts() ) : $allsearch->the_post();
// Show the posts
endwhile;
//
if ( $allsearch->max_num_pages > 1 ) : ?>
<div class="nextprev pagination">
<div class="nav-previous"><span class="nextprev-arrow">‹</span> <span class="nextprev-link-title">Older posts</span></div>
<div class="nav-next"><span class="nextprev-arrow">›</span> <span class="nextprev-link-title">Newer posts</span></div>
</div><!-- .nextprev -->
<?php endif; ?>
<?php wp_reset_query(); ?>
I've been on this problem for a week now. I've searched through countless posts that just aren't answering the question. It seems that my wp_query breaks pagination. I've looked through a lot of the post on wp_query and pagination on here but with still no joy.
As far as I can see it's set up correctly. I have one instance where it will show all posts in one page, and in the other it repeats the content on each page. See the code below for a stripped down version with notes.
Can anyone see what the problem is?
<?php
/*
Let's asume that this is the search query in the address bar:
?s=media&trend=&industry=&d=2012-05-17|2012-11-17
(To iterate, it's saying search for keyword 'media'
posted between 17th May 2012 and 17th Novemer 2012)
It gives 13 results and shows all on the same page.
I have set show 10 posts per page so it should be cutting off
at first 10.
PAGINATION: On line 56 If I change the code to
$wp_query->max_num_pages, the pagination works but then
repeats the same 10 posts.
*/
// Setup pagination
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// Sort trend taxonomy
$tr = $_GET['trend'];
// Sort industry taxonomy
$ind = $_GET['industry'];
// Set date var
$d = $_GET['d'];
$args = array(
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => 10,
'trend' => $tr,
'industry' => $ind
);
// Date range function, if available
if( (isset($_GET['d'])) && ($_GET['d']!=='') ) {
function filter_where( $where = '' ) {
$date_range = $_GET['d'];
$date_range = explode("|", $date_range);
$that_date = $date_range[0];
$this_date = $date_range[1];
$where .= " AND post_date >= '".$that_date."' AND post_date <= '".$this_date."'";
return $where;
}
// Add the filter
add_filter( 'posts_where', 'filter_where' );
}
// setup the wp_query
$allsearch = new WP_Query($args);
// Remove the date filter where clause
if( (isset($_GET['d'])) && ($_GET['d']!=='') ) {
remove_filter( 'posts_where', 'filter_where' );
}
while ( $allsearch->have_posts() ) : $allsearch->the_post();
// Show the posts
endwhile;
//
if ( $allsearch->max_num_pages > 1 ) : ?>
<div class="nextprev pagination">
<div class="nav-previous"><span class="nextprev-arrow">‹</span> <span class="nextprev-link-title">Older posts</span></div>
<div class="nav-next"><span class="nextprev-arrow">›</span> <span class="nextprev-link-title">Newer posts</span></div>
</div><!-- .nextprev -->
<?php endif; ?>
<?php wp_reset_query(); ?>
Share
Improve this question
edited Nov 17, 2012 at 21:52
fuxia♦
107k39 gold badges255 silver badges459 bronze badges
asked Nov 17, 2012 at 15:22
ziljzilj
3734 gold badges9 silver badges16 bronze badges
8
|
Show 3 more comments
1 Answer
Reset to default 0If you want to stay with a new Wp Query, as in your example, changing your $allsearch name for the query variable to $wp_query should do the trick. Looks like the pagination functions are expecting that variable name for the query... So, try that...
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745270611a4619722.html
showposts
andposts_per_page
are conflicting. They do the same thing, and the former is deprecated, but you have them set differently. I don't know it that is the problem or not, but it is something I noticed. – s_ha_dum Commented Nov 17, 2012 at 15:42print_r($allsearch)
to see the SQL that WordPress is generating. – Milo Commented Nov 17, 2012 at 16:30$limit = get_option('posts_per_page');
andposts_per_page=".$limit."
. This works but now my$wp_query->post_count
shows '10' on every page, even if 13 results are found. Almost there!! – zilj Commented Nov 17, 2012 at 16:34$wp_query->post_count
is referring to a different query. part of the issue is that you're using pagination parameters from the main query, but then creating an entirely new query. try usingpre_get_posts
to manipulate the query instead, it doesn't waste a query and will make these pagination issues go away. – Milo Commented Nov 17, 2012 at 17:37