Error 404 wordpress pagination

Asked

Viewed 100 times

0

I developed a son theme based on Spacious, the problem is that when doing a search is returned that there are for example 20 results, on the first page I preview the first 10 results, however when advancing to page 2, I get a 404 error, and there are still posts to be displayed.

Permanent link settings are as: /%category%/%postname%/

In the archive search.php I have the following search code:

$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 the pagination I make a call to the following function that is in my file functions.php:

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 left commented the attribute base, because when that line is discolored it keeps appearing #038; between the GET parameters.

The archive .htaccess is the default of wordpress.

Can someone please help me, I’ve researched several places and for now nothing...

1 answer

0


I finally managed to solve this problem.

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

Follow my code if anyone 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 makes the pagination wordpress_pagination() haven’t changed a bit.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.