按两个条件进行高级搜索-按位置和日期排序(升序或降序)

时间:2019-09-18 作者:Badan

我已经花了几天的时间来研究这个问题,但我仍然没有弄明白。我正在尝试制作高级搜索过滤器,根据两个条件对特定类别的帖子进行排序:位置(自定义字段)和发布日期(默认WordPress日期)。我希望用户能够按位置对帖子进行排序,并在按下搜索按钮之前指定帖子顺序(降序或升序)。到目前为止,我设法对位置进行了搜索,但我不知道如何将这两个标准结合起来,也不知道如何按日期对帖子进行排序:

<form name="search" action="" method="get">
  <select name="place">
    <?php
    $metakey = \'place\';
    $places = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM   $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );
    if ($places) {
       foreach ($places as $place) {
          echo "<option value=\\"" . $place . "\\">" . $place . "</option>";
       }
    }
  ?>
  </select>

  <select class="dropdown-class" name="sort-posts" id="sortbox">
      <option disabled>Sort by</option>
      <option value="&orderby=date&order=dsc">Newest</option>
      <option value="&orderby=date&order=asc">Oldest</option>
  </select>

  <input type="submit" value="Search" />
</form>

<?php
$places = $_GET[\'place\'];
if ($places) {
  $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;

  $args=array(
    \'cat\'=>6,
    \'meta_value\' => $places,
    \'paged\'=>$paged,
  );

  query_posts($args);
} else {
  query_posts(\'cat=6&posts_per_page=4\');
}

if ($places) { ?>
  <h1>Search for: <?php echo $places; ?></h3>
  <?php } else { ?>
  <h3></h3>
  <?php } ?>

<div class="content-area">

    <?php if ( have_posts() ) : ?>

        <header class="archive-header">
            <?php
                the_archive_title( \'<h1 class="archive-title">\', \'</h1>\' );
                the_archive_description( \'<div class="archive-description">\', \'</div>\' );
            ?>
        </header><!-- .page-header -->

        <?php
        while ( have_posts() ) :

            the_post();

            get_template_part( \'template-parts/content\', get_post_format() );

        endwhile;

    endif;
    ?>

</div><!-- .content-area -->
我不知道该怎么做。我在网上搜索了几个小时,但找不到任何解决办法。任何帮助都将不胜感激。

1 个回复
SO网友:Badan

我终于找到了解决办法。对于那些可能也会偶然发现这一点的人。

我要做的是将ASC和DESC作为日期排序的选项值。我在文档中读到WordPress默认使用这些值对数据进行排序。

<select class="dropdown-class" name="order" id="sortbox">
    <option value="DESC">Newest</option>
    <option value="ASC">Oldest</option>
</select>
然后,创建一个存储搜索查询值的变量:

$sortedDate=$ _GET[ \'order\']; //Either DESC or ASC
最后,我必须使用搜索查询的结果对数据库查询进行排序:

  $args=array( \'cat\'=>6, 
              \'meta_value\' => $places, 
              \'order\'=> $sortedDate, 
              \'paged\'=>$paged, ); 
  query_posts($args);
完整代码:

--高级搜索。php

<?php 
/** 
 * Template Name: Advanced Search 
 * Author: Atanas Yonkov 
 */ ?>

<form class="post-filters" name="search" action="" method="get">
    <select name="place">
        <option value="" disabled selected> Place </option>
        <?php $metakey=\'place\' ; $places=$ wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) ); if ($places) { foreach ($places as $place) { echo "
        <option value=\\ "" . $place . "\\">" . $place . "</option>"; } } ?>
    </select>

    <select class="dropdown-class" name="order" id="sortbox">
        <option value="DESC">Newest</option>
        <option value="ASC">Oldest</option>
    </select>

    <input type="submit" value="Search" />
</form>

<?php $places=$ _GET[ \'place\']; 
$sortedDate=$ _GET[ \'order\']; 

if ($places || $sortedDate) { 
  $paged=( get_query_var( \'paged\')) ? get_query_var( \'paged\') : 1; 
  $args=array( \'cat\'=>6, 
              \'meta_value\' => $places, 
              \'order\'=> $sortedDate, 
              \'paged\'=>$paged, ); 
  query_posts($args); 
} else { 
  query_posts(\'cat=6&posts_per_page=5\'); 
} 

if ($places) { ?>
<h1>Search for: <?php echo $places; ?></h3>
  <?php } else { ?>
  <h3></h3>
  <?php }
——6类。php

<?php
/**
 * The template for displaying programs
 * Implements custom advanced search filter
 */

get_header(); 
//Call the advanced-search.php template
get_template_part( \'advanced-search\', get_post_format()) ?>

    <div class="content-area">
        <?php if ( have_posts() ) : //Loop through posts ?>
            <header class="archive-header">
                <?php
                    the_archive_title( \'<h1 class="archive-title">\', \'</h1>\' );
                    the_archive_description( \'<div class="archive-description">\', \'</div>\' );
                ?>
            </header><!-- .page-header -->
            <?php
            while ( have_posts() ) :
                the_post();
                get_template_part( \'template-parts/content\', get_post_format() );
            endwhile;
        else :
            get_template_part( \'template-parts/content\', \'none\' );
        endif;
        ?>
    </div><!-- .content-area -->

<?php wp_reset_query(); ?>
<?php
get_sidebar();
get_footer();
最终的结果是,我能够根据两个不同的标准对特定类别的帖子类型进行排序。

相关推荐

Live search by custom tag

实时搜索:$the_query = new WP_Query( array( \'s\' => esc_attr( $_POST[\'keyword\'] ), \'post_type\' => \'custom_type\', \'sentence\' => \'true\' )); if( $the_query->have_posts() ) : while( $the_query->have_posts() ): $the_query->t