0

I'm building currently a website on WordPress. The site has a custom post type. I have a customized search form, which selects entries from custom post type. Entries are being selected and sorted as they are supposed to, however on my search result page (job-search.php) I need a pagination, and it's not working, although same codes works fine on other pages. I see page number changing in browser's search bar, but I always see entries from first page.

Code for search form and query for custom post type with specified selections and filtering (inside job-search.php):

<?php
/*
 * Template Name:job-search
 */
get_header();?>

<div>
    <form method="get" action="<?php echo site_url('/'); ?>/job-search" >
        <input type="search" name="job-name" placeholder="Job..." />
        <input type="search" name="town-name" placeholder="Town..." />
        <select type="search" name="date">
            <option value="DESC">Datum absteigend</option>
            <option value="ASC">Datum aufsteigend</option>      
        </select>
        <div>
            <input type="checkbox" name="cat[]" value="IT, Telekommunikation" />IT, Telekommunikation
            <input type="checkbox" name="cat[]" value="Ingenieur, Technik" />Ingenieur, Technik
            <input type="checkbox" name="cat[]" value="Handwerk, Gewerbe" />Handwerk, Gewerbe
        </div>
        <input type="submit" value="Search">
    </form>
</div>

<?php
    $jobName = $_GET['job-name'];
    $jobTown = $_GET['town-name'];
    $jobDate = $_GET['date'];

    if($_GET['cat'] && !empty($_GET['cat'])){
        $jobCat = $_GET['cat'];
    }
    else{
        $jobCat = array('Vertrieb, Verkauf', 'IT, Telekommunikation', 'Ingenieur, Technik', 'Personalwesen', 'Bildung, Soziales', 'Einkauf, Logistik', 'Handwerk, Gewerbe', 'Führungskräfte', 'Marketing, PR', 'Medizin, Pflege', 'Finanzen, Steuern, Recht', 'Kaufmännische Berufe', 'Luft- und Raumfahrt, Reise', 'Ausbildung', '');
    }
?>
<h1 style="font-size:1.5em; font-weight:bold; border-bottom:2px solid green; color:green; margin-bottom:20px;">wp_query_search results</h1>
<?php
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    $q1 = array(
        'post_type' => 'jobs',
        's' => $jobName,
        'posts_per_page' => 10,
        'paged' => $paged,
        'meta_key' => 'date',
        'orderby' => 'meta_value',
        'order' => $jobDate,
        'meta_query' => array (
            'relation' => 'AND',
            array(
                'key' => 'town',
                'value' => $jobTown,
                'compare' => 'LIKE'
            ),
            array(
                'relation' => 'OR',
                array(
                    'key' => 'cat_1',
                    'value' => $jobCat
                ),
                array(
                    'key' => 'cat_2',
                    'value' => $jobCat
                )
            )
        )
    );
    $query = new WP_Query($q1);
    if($query->have_posts()) : while($query->have_posts()) : $query->the_post();?>
        <h2 style="color:brown; font-weight:bold; border-bottom:1px dotted brown;"><?php the_field('title'); ?></h2>
        <h3><?php the_field('town'); ?></h3>
        <h4><?php the_field('cat_1'); ?> | <?php the_field('cat_2'); ?></h4>
        <h5><?php the_field('date'); ?></h5>
    <?php endwhile;

    if (function_exists(search_pagination)) {
        search_pagination($query->max_num_pages,"",$paged);
    }

    endif; wp_reset_query(); ?>

</div>
<?php get_footer(); ?>

Code for Pagination (inside functions.php):

function search_pagination($numpages = '', $pagerange = '', $paged='') {
  if (empty($pagerange)) {
    $pagerange = 2;
  }
  $paged;
  if (empty($paged)) {
    $paged = 1;
  }
  if ($numpages == '') {
    //global $wp_query;
    //$numpages = $wp_query->max_num_pages;
    $numpages = $query->max_num_pages;
    if(!$numpages) {
        $numpages = 1;
    } 
  }
  $pagination_args = array(
    'base'            => get_pagenum_link(1) . '%_%',
    'format'          => '/page/%#%',
    'total'           => $numpages,
    'current'         => $paged,
    'show_all'        => False,
    'end_size'        => 1,
    'mid_size'        => $pagerange,
    'prev_next'       => True,
    'prev_text'       => __('&laquo;'),
    'next_text'       => __('&raquo;'),
    'type'            => 'plain',
    'add_args'        => false,
    'add_fragment'    => ''
  );

  $paginate_links = paginate_links($pagination_args);

  if ($paginate_links) {
    echo "<nav class='custom-pagination'>";
      echo $paginate_links;
    echo "</nav>";
  }
}

I truly need help and advices. Thanks a lot in advance!

2

From your job-search.php replace 'posts_per_page' => 10, to 'posts_per_page' => 3,

and Simply add this following code in your search page, i mean where you are searching the posts.

the_posts_pagination( array( 'mid_size'  => 2 ) );
-1

Everything looks like things I've used in a similar fashion. The only thing, is, I believe the function_exists parameter needs to pass a string. It should be:

if ( function_exists('search_pagination') ) {

Looks like the way you had it wouldn't register it as a string.

  • Hi, thanks for answering, but it didn't solve anything. The problem still persists. Got any other idea? – RomanT Jan 16 '17 at 21:14
  • 1
    The problem with custom pagination is usually the fact that the "paged" value gets dumped entirely, so get_query_var might be your weak point. You may need to request it in another way. – socki03 Jan 16 '17 at 21:18

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.