我正在建立一个酒店搜索网站,并希望在用户搜索某个位置时按自定义字段过滤我的搜索结果。因此,例如当用户输入“伦敦”时,它将根据他们添加的一些自定义字段在伦敦找到酒店。但只有当我搜索example.com/?s=
这样的位置时,它才会在搜索example.com/?s=London
时有效,而我的分页没有显示。但是,如果我没有添加任何位置并将搜索保留为空白,例如example.com/?s=
分页显示并且正常工作。谁会知道为什么?我在php上不是那么棒,所以我真的需要一些帮助。我当前的search.php文件看起来像这样。
<?php $unique_id = esc_attr( uniqid( 'search-form-' ) ); ?>
<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<input type="search" id="<?php echo $unique_id; ?>" class="search-field" value="<?php echo get_search_query(); ?>" name="s" />
<button type="submit" class="search-submit">Search</button>
</form>
<?php $searchm = get_search_query(); ?>
<?php if ( get_search_query() == '' ): ?>
<?php
$args_a = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'CurrentLocation',
'value' => array($areauserwants, areauserwants2),
'compare' => 'IN',
),
array(
'key' => 'AreaLookingFor',
'value' => $areaihave,
'type' => '',
'compare' => '=',
),
array(
'key' => 'NumberOfBeds',
'value' => $howmanybedsdoiwant,
'type' => '',
'compare' => 'IN',
),
array(
'key' => 'TypeOfHotel',
'value' => $hoteltype,
'type' => '',
'compare' => 'IN',
),
),
'paged' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1,
); ?>
<?php $query_a = new WP_Query( $args_a ); ?>
<?php else : ?>
<?php
$args_a = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'CurrentLocation',
'value' => $searchm,
'compare' => '=',
),
array(
'key' => 'AreaLookingFor',
'value' => $areaihave,
'type' => '',
'compare' => '=',
),
),
'paged' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1,
); ?>
<?php $query_a = new WP_Query( $args_a ); ?>
<?php endif; ?>
<?php if ( $query_a->have_posts() ) : ?>
<ul>
<!-- the loop -->
<?php while ( $query_a->have_posts() ) : $query_a->the_post(); ?>
<?php get_template_part( 'template-parts/post/content', 'excerpt' ); ?>
<?php endwhile; ?>
<!-- end of the loop -->
</ul>
<?php next_posts_link( 'Next', $custom_query->max_num_pages );
previous_posts_link( 'Previous' ); ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'No homes matched your criteria' ); ?></p>
<?php endif; ?>