0

I have a pagination code that works fine in a custom post type pages single-category.php

/*
Template Name: categorie page
Template Post Type: post
*/

but is not working in custom pages template 'page-search.php'

/*
Template Name: Search
Template Post Type: page
*/

The pagination in the single-category works fine, but in the page-search page (which has the slug 'search') when I try to go to the second page, the url changes to 'localhost/search/2' but the displayed content is the (home page content) instead of the (second results search page content).

I'm using the same code for both pages.

$paged = 1;
if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); }
elseif ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); }
else { $paged = 1; }
//$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array (  
    'post_type'      => 'product',
    'post_status' => 'publish',  
    'posts_per_page' => 1,
    'paged' => $paged,
    'post_status' => 'publish',
    'meta_query' => array(
        'relation' => 'AND',
    )
);
if(isset($_GET['q'])){
    $args['meta_query'][] = array(
        'relation' => 'OR',
        array(
            'key' => 'order_code',
            'value' => $_GET['q'],
            'compare' => 'LIKE',
        ),
        array(
            'key' => 'market_category',
            'value' => $_GET['q'],
            'compare' => 'LIKE',
        ),
        array(
            'key' => 'box_quantity',
            'value' => $_GET['q'],
            'compare' => 'LIKE',
        ),
    );
}
if(isset($_GET['type'])){
    if($_GET['type']==1){
        $args['meta_query'][] = array(
            'key' => 'market_category',
            'value' => 'Food',
            'compare' => '=',
        );
    }
    else if($_GET['type']==2){
        $args['meta_query'][] = array(
            'key' => 'market_category',
            'value' => 'non-Food',
            'compare' => '=',
        );
    }
}
$the_query  = new WP_Query($args);
$posts_array = $the_query->posts;
//display posts here

and for displaying the navigation links

$pages = paginate_links( array(
    //'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
    'base'         => esc_url(get_permalink()) . '%#%',
    'total'        => $the_query->max_num_pages,
    'current'      => $paged,
    'format'       => '?paged=%#%',
    'show_all'     => false,
    'type'         => 'plain',
    'end_size'     => 2,
    'mid_size'     => 1,
    'prev_next'    => true,
    'prev_text'    => sprintf( '<i></i> %1$s', '«' ),
    'next_text'    => sprintf( '%1$s <i></i>', '»' ),
    'add_args'     => false,
    'add_fragment' => '',
) );
$pages = explode("\n", $pages);




<div aria-label="Page navigation example">
    <ul class="pagination text-center">
        <?php foreach ($pages as $key):?>
        <li class="page-item"><?=str_replace(
            array('page/', 'page-numbers'),
            array('', 'page-link'),
            $key
            )?></li>
        <?php endforeach;?>
    </ul>
</div>
  • es_AR: Estas usando una pagina, ¿ para mostrar resultados de búsqueda ? en_GogleTranslate: Are you using a page, to show search results? – Salem Terrano Jun 12 '18 at 10:12
  • Yes. I'm trying to using a page to show the search results. – Yazan W Yusuf Jun 12 '18 at 10:31
  • why are you removing page/ from the pagination URLs? can you check, what localhost/search/page/2 gives..? ps try to avoid php short tags (aka. <?= in favor of <?php echo ) as this might bring you troubles on different php settings – honk31 Jun 12 '18 at 12:01
  • When I add /{custom_post_type}/{post_title}/page/{page number} in the custom post type pagination it redirects me to /{custom_post_type}/{post_title}. But when I open the link /search/page/{page_number} it displays the content of the main page, not the content of the search page. – Yazan W Yusuf Jun 12 '18 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.