Error 404 wordpress paging

0

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

    
asked by anonymous 12.09.2018 / 19:19

1 answer

0

I finally solved this problem.

Basically what I did was remove all the search logic that was in the file search.php and put it in the file functions.php , for that I used add_action( 'pre_get_posts', 'your_function_name' ); .

Below is my code if someone else has this same problem:

add_action( 'pre_get_posts', 'search_main_query', 10, 2 );
function search_main_query( $query ) {

    if ( is_search() && $query->is_main_query() && !is_admin() ) {

        $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;

        if( $is_author || $is_keywords ){
            // we have to remove the "s" parameter from the query, because it will prevent the posts from being found
            $query->query_vars['s'] = "";
        }

        $query->set( 'post_status', 'publish');
        // Search post types
        $query->set( 'post_type', $types );
        // Search meta fields.
        $query->set( 'meta_query', $post_meta );
    }
}

The function that does pagination wordpress_pagination() has not changed anything.

Whenever you need to implement an advanced search, I recommend using this add_action( 'pre_get_posts', 'your_function_name' ); action, because placing the search logic in the search.php file may cause this bug.

    
14.09.2018 / 20:35