0

I have my search set up with a page called 'search' and a template called 'searchpage.php'.

When I visit 'domain.com/search?s=term' the URL changes to 'domain.com/search/?s=term'

To solve this and make it work I used:

add_rewrite_endpoint( 'search', EP_ALL );

However, my pagination links im creating use a URL structure of:

'domain.com/search?s=term&paged=3'

WP changes this URL to:

'domain.com/search/page/3/?s=term'

Which still shows a search page but the pagination doesn't work as it would if it kept the normal paged=x.

Any advice?

1 Answer 1

0

If your are using a custom template I assume you are using a custom loop so first populate $paged.

<?php $max_posts = get_option('posts_per_page '); ?>
<?php global $paged; ?>
<?php 
  if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
  elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
  else { $paged = 1; }  
?>

Then use $paged in your own loop;

  <?php $args = array('posts_per_page' => $max_posts,
                      'paged' => $paged ); ?>
  <?php $query = new WP_Query( $args ); ?>          
  <?php if ( $query->have_posts() ) : ?>            
    <?php while ( $query->have_posts() ) : $query->the_post(); ?>        
    <article>
      Your code here
    </article>      
    <?php endwhile ?>    
  <?php endif; ?>  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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