Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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">&lsaquo;</span> <span class="nextprev-link-title">Older posts</span></div>
    <div class="nav-next"><span class="nextprev-arrow">&rsaquo;</span> <span class="nextprev-link-title">Newer posts</span></div>
    </div><!-- .nextprev -->
<?php endif; ?>
<?php wp_reset_query(); ?>
share|improve this question
1  
showposts and posts_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 Nov 17 '12 at 15:42
    
Iv'e corrected this. And thanks but no joy. Any other ideas? –  Graeme Nov 17 '12 at 15:50
    
what does the URL look like for the second page? have you inspected the value of the paged query var to be sure it's what you expect? also print_r($allsearch) to see the SQL that WordPress is generating. –  Milo Nov 17 '12 at 16:30
    
I checked all of that, but i managed to almost get it working by adding in $limit = get_option('posts_per_page'); and posts_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!! –  Graeme Nov 17 '12 at 16:34
4  
$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 using pre_get_posts to manipulate the query instead, it doesn't waste a query and will make these pagination issues go away. –  Milo Nov 17 '12 at 17:37

1 Answer 1

If 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...

share|improve this answer
    
Please use formatting for code. –  kaiser Dec 1 '12 at 17:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.