我终于找到了解决办法。对于那些可能也会偶然发现这一点的人。
我要做的是将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();
最终的结果是,我能够根据两个不同的标准对特定类别的帖子类型进行排序。