3

I am using custom search form and custom template to display the results according to the solution given in here Everything is working perfect except the pagination which doesn't seem to work at all

Overview

  • Custom post type : job_listing
  • Custom taxonomy : job_listing_type
  • custom fields as key: _job_location

Here is the code for search form :

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

        <select id="my-select1" class="js-example-basic-single isotopeselect" data-filter-group="proyectos" multiple="multiple" name="terms[]">

        <?php foreach ($job_terms as $job_term): ?>
          <option value=".<?php echo str_replace(' ', '-', str_replace('&', 'and', html_entity_decode($job_term)));?>"><?php echo $job_term;?></option>
        <?php endforeach; ?>

        </select>    
        <button type="submit"><span>search</span></button>
    <input type="hidden" name="job_listing" value=""/>
</form>

Here is the search result template

<?php 
get_header();
$terms=array();
$all=($_REQUEST); 


if(isset($_REQUEST["terms"])){
    $myterms=$all["terms"];
    foreach ($myterms as $myterm) {
    $myterm=str_replace(".","",$myterm);
    $terms[]=$myterm;    
    }
}

if($terms){
    $tax_query[] =  array(
                        array(
                        'taxonomy' => 'job_listing_type',
                        'field' => 'name',
                        'terms' => $terms
                      ) );
}

$post_type = 'job_listing';
$paged= (get_query_var('paged' )) ? get_query_var('paged'):1;
$args         =  array(
    'paged'=>$paged,
    'post_type'=>$post_type,
    'posts_per_page'=>1, 
    'tax_query' => $tax_query,
    );
$loop = new WP_Query( $args );     
if($loop->have_posts()):?>

    <div class="my-services">
        <?php    
        while($loop->have_posts()):$loop->the_post();              
        the_title();

        endwhile;
        ?>

    </div>
<?php endif; ?>
    <div class="pagination" id="blog-pagination">
      <span class="previous" ><?php previous_posts_link( '&larr;Newer', $loop->max_num_pages  ); ?></span>
      <span class="next"><?php next_posts_link( 'Older &rarr;', $loop->max_num_pages  ); ?></span>
    </div>
    <?php wp_reset_query();?>     

<?php get_footer(); ?>

Result on initial search, url is like

http://example.com/?search=advanced&terms%5B%5D=.Eat&job_listing=

After i hit next then url becomes

http://example.com/page/2/?search=advanced&terms%5B0%5D=.Eat&job_listing

but nothing happens, result remains same

I have never done this before so please help me out on this

  • 1
    The other answer on that question is actually a much better solution, it modifies the main query rather than generating a new one in the template, which is always fraught with disaster when dealing with pagination. – Milo Apr 23 '17 at 22:36
  • @Milo I am also thinking of using that solution but somehow correct template heirarchy is not working as explained in other problem here wordpress.stackexchange.com/questions/264648/… – aryan Apr 24 '17 at 7:21
  • That sounds like a problem with the plugin and something you may need to take up with their support. – Milo Apr 24 '17 at 16:28
1

i've seen this: next_posts_link

You must use wp_reset_postdata when use new WP_QUERY(), and this

<div class="pagination" id="blog-pagination">
  <span class="previous" ><?php previous_posts_link( '&larr;Newer', $loop->max_num_pages  ); ?></span>
  <span class="next"><?php next_posts_link( 'Older &rarr;', $loop->max_num_pages  ); ?></span>
</div>

go into if($loop->have_posts()):?> condition

Your Answer

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

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