the theme I made didn't have a search functionality. I started receiving in Search Console thousands of soft 404 for similar pages:
https://www.example.com/?s={search_term_string}page/3page/2page/3page/2page/3page/3page/3page/2page/2page/3page/3page/3page/2page/3page/2
So I created a new search.php
page. The pagination is working fine, it is showing the right amount of pages and it paginates well.
However the links are wrong
.
Page 1 is: https://www.example.com/?s=therapy
- which is good
Then I click on page 2:
Page 2 is: https://www.example.com/?s=therapy&paged=2/page/2/
- which is not the format I want, but it works.
Page 3 is https://www.example.com/?s=therapy&paged=3/page/3/
- same situation
If the page "1" button now has link https://www.example.com//page/1/
- which is wrong and redirects to https://www.example.com
Questions:
How to make the links of page 2 and next pages to have a working structure of https://www.example.com/?s=therapy/page/3/
?
How to make the link to page one be https://www.example.com/?s=therapy
?
This is the code I have:
<?php
get_header();
$args_booking = array(
'posts_per_page' => 2,
'post_type' => 'post',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1
);
$query = new WP_Query($args_booking);
?>
[post loop here]
<div class="pagination">
<?php
$total_pages = $wp_query->max_num_pages;
if ($total_pages > 1) :
$current_page = max(1, get_query_var('paged'));
$search_query = get_search_query();
$base = esc_url(home_url('/')) . '%_%';
$base = user_trailingslashit($base) . 'page/%#%/';
echo paginate_links(array(
'base' => $base,
'format' => '?s=' . urlencode($search_query) . '&paged=%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => '‹',
'next_text' => '›',
));
endif;
?>
</div>
<?php } ?>
If I change it this way, the URLs look fine, but the pagination doesn't work... which means that no matter the page, always the same posts are being shown.
<div class="pagination">
<?php
$total_pages = $wp_query->max_num_pages;
if ($total_pages > 1) :
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => '‹',
'next_text' => '›',
));
endif;
?>
</div>
<?php } ?>
Can anyone please help?
Update. Here is the code after the changes suggested in the first answer:
<?php get_header(); ?>
<div class="wapper">
<div class="contentarea clearfix">
<div class="content">
<h1 class="search-title"> <?php echo $wp_query->found_posts; ?>
<?php _e( 'Search Results Found For', 'locale' ); ?>: "<?php the_search_query(); ?>" </h1>
<?php if ( have_posts() ) { ?>
<ul>
<?php while ( have_posts() ) { the_post(); ?>
<li>
<h3><a href="<?php echo get_permalink(); ?>">
<?php the_title(); ?>
</a></h3>
<?php the_post_thumbnail('medium') ?>
<?php echo substr(get_the_excerpt(), 0,200); ?>
<div class="h-readmore"> <a href="<?php the_permalink(); ?>">Read More</a></div>
</li>
<?php } ?>
</ul>
<?php paginate(); } ?>
</div>
</div>
</div>
<?php get_footer(); ?>
and code added to the bottom of functions.php
:
/**
* Override search query parameters
*
* @param object $query An instance of WP_Query
*/
function wpse_424907( $query ) {
if( $query-> is_main_query() && $query->is_search() ) {
$query->set( 'posts_per_page', 2 );
$query->set( 'post_type', 'post' );
return;
}
}
add_action( 'pre_get_posts', 'wpse_424907', 1 );
/**
* Echo pagination. If used with a custom query, it needs to be passed as an argument. Otherwise it assumes the default $wp_query
*
* @param object $query An instance of WP_Query
*/
function paginate($query = '') {
if (!($query instanceof WP_Query)) {
global $wp_query;
$query = $wp_query;
}
if ($query->max_num_pages > 1) {
$current_page = max(1, get_query_var('paged'));
echo '<nav class="pagination">';
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'current' => $current_page,
'total' => $query->max_num_pages,
'prev_text' => '‹',
'next_text' => '›'
));
echo '</nav>';
}
}