我确实按帖子类型(艺术家、事件、帖子)对搜索结果进行了分组,并将每个结果放在一个容器中。下一步是在容器中为每个容器指定自己的顺序:
艺术家-来自A-Z活动-所有即将到来的活动首先发布-日期描述我知道如何为每个人设置查询,但我不知道如何在我已有的代码中使其发挥功能。。。谁能帮我?
这是我的searchresults中的一个片段。php:
<?php
$last_type="";
$typecount = 0;
while (have_posts()) :
the_post();
if ($last_type != $post->post_type){
$typecount = $typecount + 1;
if ($typecount > 1){
echo \'</div><!-- close container -->\'; //close type container
}
// save the post type.
$last_type = $post->post_type;
//open type container
switch ($post->post_type) {
case \'cpt_artist\':
echo "<div class=\\"artistsearch-container\\"><h2>Artiesten</h2>";
break;
case \'cpt_event\':
echo "<div class=\\"eventsearch-container\\"><h2>Evenementen</h2>";
break;
case \'post\':
echo "<div class=\\"postsearch-container\\"><h2>Blog Results</h2>";
break;
}
}
?>
<?php if(\'post\' == get_post_type()) : ?>
<h2 class="blog">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h2>
<?php endif; ?>
<?php if(\'cpt_event\' == get_post_type()) :
$event_date = get_post_meta( get_the_ID(), \'ci_cpt_event_date\', true );
$event_time = get_post_meta( get_the_ID(), \'ci_cpt_event_time\', true );
$event_timestamp = strtotime( $event_date . \' \' . $event_time, current_time( \'timestamp\' ) );
$event_enddate = get_post_meta( get_the_ID(), \'ci_cpt_event_enddate\', true );
$event_endtime = get_post_meta( get_the_ID(), \'ci_cpt_event_endtime\', true );
$event_endtimestamp = strtotime( $event_enddate . \' \' . $event_endtime, current_time( \'timestamp\' ) );
$event_location = get_post_meta( get_the_ID(), \'ci_cpt_event_location\', true );
$event_venue = get_post_meta( get_the_ID(), \'ci_cpt_event_venue\', true );
$event_timestamp = strtotime( $event_date . \' \' . $event_time, current_time( \'timestamp\' ) );
?>
<div class="search-results-item-event"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br/>
<?php if (empty($event_enddate)){?>
<span><?php echo date_i18n( get_option(\'date_format\'), $event_timestamp ); ?> · <?php echo $event_venue; ?>, <?php echo $event_location; ?></span>
<?php
;}
else {?>
<span><?php echo date_i18n( get_option(\'date_format\'), $event_timestamp ); ?> - <?php echo date_i18n( get_option(\'date_format\'), $event_endtimestamp ); ?> · <?php echo $event_venue; ?>, <?php echo $event_location; ?></span>
<?php
;}
?>
</div>
<?php endif; ?>
<?php if(\'cpt_artist\' == get_post_type()) : ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endif; ?>
<?php endwhile; ?>
<?php else : ?>
<div class="open-a-div">
<p>No results found.</p>
<?php endif; ?>
</div>
<?php ci_pagination(); ?>
</div>
这是我的函数的一个片段。php:
add_filter(\'posts_orderby\', \'group_by_post_type\', 10, 2);
function group_by_post_type($orderby, $query) {
global $wpdb;
if ($query->is_search) {
return $wpdb->posts . \'.post_type ASC\';
}
// provide a default fallback return if the above condition is not true
return $orderby;
}