Developed a child theme based on the Spacious , the problem is that doing a search is returned that exists for example 20 results, on the first page I see the first 10 results, however when I go to page 2, I get a 404 error, and there are still posts to be displayed.
Permanent link settings are like: /%category%/%postname%/
In the file search.php
I have the following code to search:
$metas = array();
$is_author = false;
$is_keywords = false;
if( isset($_GET['searchin']) && !empty($_GET['searchin']) ){
if($_GET['searchin'] == 'articles'){
$types = array('article');
}
else if($_GET['searchin'] == 'articles-author'){
$is_author = true;
$types = array('article');
$metas[] = array(
'key' => 'wpcf-autores',
'value' => get_search_query(),
'compare' => 'LIKE',
);
}
else if($_GET['searchin'] == 'articles-keywords'){
$is_keywords = true;
$types = array('article');
$metas[] = array(
'key' => 'keywords',
'value' => get_search_query(),
'compare' => 'LIKE',
);
}
}
else{
$types = array('post', 'page');
}
$post_meta = $metas;
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$default_posts_per_page = get_option( 'posts_per_page' );
$args = array(
'paged' => $paged,
'posts_per_page' => $default_posts_per_page,
'post_status' => 'publish',
'post_type' => $types,
'meta_query' => $post_meta
);
if( !$is_author && !$is_keywords ){
$args['s'] = get_search_query();
}
// The Query
$posts = new WP_Query( $args );
set_query_var( 'max_num_pages', $posts->max_num_pages );
To display pagination I make a call to the following function that is in my functions.php
file:
function wordpress_pagination(){
global $wp_query;
$max_num_pages = get_query_var( 'max_num_pages', null ); //value set in search.php
if( is_null($max_num_pages) ){
$max_num_pages = $wp_query->max_num_pages;
}
$big = 999999999;
if( $max_num_pages > 1){
echo paginate_links(array(
//'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => 'page/%#%/',
'current' => max( 1, get_query_var('paged') ),
'total' => $max_num_pages,
'prev_text' => '<i class="fa fa-angle-double-left" aria-hidden="true"></i>',
'next_text' => '<i class="fa fa-angle-double-right" aria-hidden="true"></i>',
));
}
set_query_var( 'max_num_pages', null );
}
I have commented on the base
attribute, because when this line is uncommented, #038;
appears between the GET parameters.
The file .htaccess
is the default for wordpress.
Someone can help me please, I already researched in several places and for now nothing ...