1

I am trying to create custom search on my website and everything goes okay except one problem - no refresh content on pagination.

Here is what i am doing.

My searchform:

<form method="get" id="advanced-searchform" role="search" action="<?php echo esc_url( home_url( '/' ) ); ?>">

<h3><?php _e( 'Search', 'bmpl' ); ?></h3>

<input type="hidden" name="search" value="advanced">

<label for="name"><?php _e( 'Keyword: ', 'bmpl' ); ?></label><br>
<input type="text" value="" placeholder="<?php _e( 'Keyword:', 'bmpl' ); ?>" name="name" id="name" />

<input type="submit" id="searchsubmit" value="Go" />

Here is sample function in functions to load custom search result on specific template

function wpse_load_custom_search_template(){
if( isset($_REQUEST['search']) == 'advanced' ) {
    require('advanced-search-result.php');
    die();
}}add_action('init','wpse_load_custom_search_template');

And now the last - search result template

<div class="main-content-wrap" style="color:white;">

    <div class="info-content clearfix">
        <div class="col-md-12 col-sm-12 col-xs-12">
            <div class="row">

            <?php                 

            $_name = $_GET['name'] != '' ? $_GET['name'] : '';
            $current_page = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $args = array(
                    's'             =>  $_name, 
                    'posts_per_page' => 6,
                    'paged'          => $current_page
                );
            $wp_query = new WP_Query( $args );?>
            <?php if ($wp_query->have_posts() ) : ?>

            <?php while ($wp_query->have_posts() ) : $wp_query->the_post(); ?>

                <div class="col-md-4 col-sm-6 col-xs-6 col-ts-12">
                    <a href="<?php echo get_permalink() ?>">
                        <div class="news-item-card">
                            <div class="news-item-img" >
                                <h3><?php the_title(); ?></h3>
                            </div>
                        </div>
                    </a>
                </div>
            <?php endwhile; ?>

            </div>
        </div>
    </div>

   <?php kama_pagenavi('', '', true, array(), $wp_query); ?>
    <?php wp_reset_postdata();?> 
        <?php else: ?>
            <p class="not_found">Nothing found<a href="<?php echo get_home_url(); ?>"> Home</a></p>
        <?php endif; ?>     

</div>

Kama pagenavi - function for pagination, works fine on all not-search pages on the same site, where search should work. Code of pagination function:

function kama_pagenavi( $before = '', $after = '', $echo = true, $args = array(), $wp_query = null ) { if( ! $wp_query ){ wp_reset_query(); global $wp_query; }

$default_args = array(
    'text_num_page'   => '',
    'num_pages'       => 10,
    'step_link'       => 10, 
    'dotright_text'   => '…',
    'dotright_text2'  => '…', 
    'back_text'       => '« back', 
    'next_text'       => 'next»', 
    'first_page_text' => '« start', 
    'last_page_text'  => 'end»', 

$default_args = apply_filters('kama_pagenavi_args', $default_args );

$args = array_merge( $default_args, $args );

extract( $args );

$posts_per_page = (int) $wp_query->get('posts_per_page');
$paged          = (int) $wp_query->get('paged');
$max_page       = $wp_query->max_num_pages;

if( $max_page <= 1 )
    return false;

if( empty( $paged ) || $paged == 0 )
    $paged = 1;

$pages_to_show = intval( $num_pages );
$pages_to_show_minus_1 = $pages_to_show-1;

$half_page_start = floor( $pages_to_show_minus_1/2 );
$half_page_end = ceil( $pages_to_show_minus_1/2 ); 

$start_page = $paged - $half_page_start; 
$end_page = $paged + $half_page_end;

if( $start_page <= 0 )
    $start_page = 1;
if( ($end_page - $start_page) != $pages_to_show_minus_1 )
    $end_page = $start_page + $pages_to_show_minus_1;
if( $end_page > $max_page ) {
    $start_page = $max_page - $pages_to_show_minus_1;
    $end_page = (int) $max_page;
}

if( $start_page <= 0 )
    $start_page = 1;
$out = '';
$link_base = str_replace( 99999999, '___', get_pagenum_link( 99999999 ) );
$first_url = get_pagenum_link( 1 );
if( false === strpos( $first_url, '?') )
    $first_url = user_trailingslashit( $first_url );

$out .= $before . "<div class='wp-pagenavi'>\n";

    if( $text_num_page ){
        $text_num_page = preg_replace( '!{current}|{last}!', '%s', $text_num_page );
        $out.= sprintf( "<span class='pages'>$text_num_page</span> ", $paged, $max_page );
    }
    if ( $back_text && $paged != 1 )
        $out .= '<a class="prev" href="'. ( ($paged-1)==1 ? $first_url : str_replace( '___', ($paged-1), $link_base ) ) .'">'. $back_text .'</a> ';
    if ( $start_page >= 2 && $pages_to_show < $max_page ) {
        $out.= '<a class="first" href="'. $first_url .'">'. ( $first_page_text ? $first_page_text : 1 ) .'</a> ';
        if( $dotright_text && $start_page != 2 ) $out .= '<span class="extend">'. $dotright_text .'</span> ';
    }
    for( $i = $start_page; $i <= $end_page; $i++ ) {
        if( $i == $paged )
            $out .= '<span class="current">'.$i.'</span> ';
        elseif( $i == 1 )
            $out .= '<a href="'. $first_url .'">1</a> ';
        else
            $out .= '<a href="'. str_replace( '___', $i, $link_base ) .'">'. $i .'</a> ';
    }
    $dd = 0;
    if ( $step_link && $end_page < $max_page ){
        for( $i = $end_page+1; $i<=$max_page; $i++ ) {
            if( $i % $step_link == 0 && $i !== $num_pages ) {
                if ( ++$dd == 1 )
                    $out.= '<span class="extend">'. $dotright_text2 .'</span> ';
                $out.= '<a href="'. str_replace( '___', $i, $link_base ) .'">'. $i .'</a> ';
            }
        }
    }
    // в конец
    if ( $end_page < $max_page ) {
        if( $dotright_text && $end_page != ($max_page-1) )
            $out.= '<span class="extend">'. $dotright_text2 .'</span> ';
        $out.= '<a class="last" href="'. str_replace( '___', $max_page, $link_base ) .'">'. ( $last_page_text ? $last_page_text : $max_page ) .'</a> ';
    }
    // вперед
    if ( $next_text && $paged != $end_page )
        $out.= '<a class="next" href="'. str_replace( '___', ($paged+1), $link_base ) .'">'. $next_text .'</a> ';

$out .= "</div>". $after ."\n";

$out = apply_filters('kama_pagenavi', $out );

if( $echo )
    return print $out;

return $out;

}

But even if use something simple, like next\prev links - still no fine working.

Hope someone can help me, because this issue is driving me crazy on 3rd site in a row, can not understand what is going wrong and problem slowly moves me to insanity.

Sorry for big length and bad english

1 Answer 1

0

Changed architecture and code a while. Simplified example:

normal form, for example with search keyword inside some templete.

<form method="get" id="search_form" action="/results">
 <input type="text" name="search_text" id="search_text_input" placeholder="Type something for search">
 <button type="submit">GO</button>
</form>

After that i create results.php Here is code:

 <?php /* Template name: Custom Search */ 
  get_header(); 
 ?>
 <?php 
  if($_GET['search_text'] && !empty($_GET['search_text']) ) 
   {
    $text = $_GET['search_text'];
   }
  ?>
<div class="wrap" style="color:white;">
    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">
        <?php
            $big = 999999999; 
            $paged = get_query_var('paged') ? get_query_var('paged') : 1;
            $args = array(
                's' => $text,
                'posts_per_page' => 2,
                'paged' => $paged,
            );
            $my_query = new WP_Query($args);
        ?>
            <?php if ($my_query->have_posts()): ?>
            <?php while($my_query -> have_posts()) : $my_query -> the_post(); ?>
            <div class="post">
                <h2><?php the_title(); ?></h2>
            </div>
        <?php endwhile;
            echo paginate_links( array(
                            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                            'format' => '?paged=%#%',
                            'current' => max( 1, get_query_var('paged') ),
                            'total' => $my_query->max_num_pages
                            ) );
            wp_reset_postdata();
        ?>
    <?php else: ?>
        <p>Nothing to show u, human.</p>
    <?php endif; ?>

        </main>
    </div>
</div>

<?php get_footer();?>

After form i create new page in admin dashboard, called "Search results" with "results" slug with template "Advanced Search" from previously created results.php. And that`s all. More custom field can be easily added, and if needed to someone i can post way to do this here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.