自定义查询无效的搜索结果页面上的分页(WordPress)

时间:2017-01-15 23:29:17

标签: php wordpress pagination search-form

我目前在WordPress上建立了一个网站。该网站有自定义的帖子类型。我有一个自定义搜索表单,从自定义帖子类型中选择条目。正在按照预期选择和排序条目,但是在我的搜索结果页面上(job-search.php)我需要分页,但它不起作用,尽管相同的代码在其他页面上工作正常。我在浏览器的搜索栏中看到页码更改,但我总是看到第一页的条目。

搜索表单的代码和查询具有指定选择和过滤的自定义帖子类型(在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(); ?>

分页代码(在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>";
  }
}

0 个答案:

没有答案