分离自定义帖子搜索结果

时间:2011-04-15 作者:Zach Shallbetter

我有一个网站,有一些自定义的帖子类型。当客户搜索时,它会显示所有不同帖子的组合。我想在同一页上对这两个部分进行分段,得到如下示例所示的结果。任何帮助都将不胜感激。

Showing results for: really cool search
Video Management
从中心位置。Radius提供交互式地图功能和多监视器支持。专为多监视器环境设计。。。http://openeye.net/products/software/video-management

<小时/>Knowledge Base Entries:
Repairing your device
从中心位置。Radius提供交互式地图功能和多监视器支持。专为多监视器环境设计。。。http://openeye.net/?faq=video-management-repair

我的循环来自循环搜索的示例。php

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h4 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( \'Permalink to %s\', \'twentyten\' ), the_title_attribute( \'echo=0\' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h4>
<?php if ( is_archive() || is_search() ) : // Only display excerpts for archives and search. ?>
    <div class="entry-summary">
        <?php the_post_thumbnail; ?>
        <?php if (function_exists(\'relevanssi_the_excerpt\')) { relevanssi_the_excerpt(); }; ?>
        <a href="<?php the_permalink(); ?>"><?php the_permalink(); ?></a>
    </div><!-- .entry-summary -->
<?php else : ?>
    <div class="entry-content">
        <?php the_post_thumbnail; ?>
        <?php the_content( __( \'Continue reading\', \'twentyten\' ) ); ?>
        <?php wp_link_pages( array( \'before\' => \'<div class="page-link">\' . __( \'Pages:\', \'twentyten\' ), \'after\' => \'</div>\' ) ); ?>
        <a href="<?php the_permalink(); ?>"><?php the_permalink(); ?></a>
    </div><!-- .entry-content -->
<?php endif; ?>
我对搜索的更改。php

<?php if ( have_posts() ) : $posts_per_page = 20; ?>
    <span style="font-size:18px;" class="page-title"><?php printf( __( \'Showing results for: %s\', \'twentyten\' ), \'<span>\' . get_search_query() . \'</span>\' ); ?></span>
    <?php
    /* Run the loop for the search to output the results.
     * If you want to overload this in a child theme then include a file
     * called loop-search.php and that will be used instead.
     */
     get_template_part( \'loop\', \'search\' );
    ?>
<?php else : ?>
    <div id="post-0" class="post no-results not-found">
        <h2 class="entry-title"><?php _e( \'Nothing Found\', \'twentyten\' ); ?></h2>
        <div class="entry-content">
    <?php if (function_exists(\'relevanssi_didyoumean\')) {
        relevanssi_didyoumean(get_search_query(), "<p>Sorry, but nothing matched your search criteria. Please try again with some different keywords. Or you can also try ", "</p>", 5);
    }?>
            <?php related_searches(); ?>
        </div><!-- .entry-content -->
    </div><!-- #post-0 -->
<?php endif; ?>

3 个回复
最合适的回答,由SO网友:Bainternet 整理而成

您可以使用按类型对帖子分组posts_groupby 过滤器挂钩:

add_filter(\'posts_groupby\', \'group_by_post_type\' );
function group_by_post_type( $groupby )
{
  global $wpdb;
  if( !is_search() ) {
    return $groupby;
  }
  $mygroupby = "{$wpdb->posts}.post_type";
  if( !strlen(trim($groupby))) {
    // groupby was empty, use ours
    return $mygroupby;
  }
  // wasn\'t empty, append ours
  return $groupby . ", " . $mygroupby;
}
然后在您的循环中,执行以下操作:

$last_type="";
$typecount = 0;
while (have_posts()){
    the_post();
    if ($last_type != $post->post_type){
        $typecount = $typecount + 1;
        if ($typecount > 1){
            echo \'</div>\'; //close type container
        }
        // save the post type.
        $last_type = $post->post_type;
        //open type container
        switch ($post->post_type) {
            case \'post\':
                echo "<div class=\\"post container\\"><h2>posts search result</h2>";
                break;
            case \'page\':
                echo "<div class=\\"page container\\"><h2>pages search result</h2>";
                break;
            case \'custom_type_name\':
                echo "<div class=\\"custom container\\"><h2>custom post type search result</h2>";
                break;
            //add as many as you need.
        }
    }

    //...
    // do your loop 
    //...
}

SO网友:Aaron

只是想补充一点,虽然@bainternet的投票结果很好,但它可以破坏媒体库的搜索功能。这个问题和一个简单的解决方案都出现了here.

简单的解决方案是围绕上述代码添加此条件:

if (!is_admin()) {
  [your code here]
}

SO网友:acrane

在对上述解决方案稍加调整后,我实现了这一点。首先,需要更改筛选器以检索多个结果:

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 DESC\';
}
// provide a default fallback return if the above condition is not true
return $orderby;
}
然后需要稍微调整一下循环。我想用不同的信息来设计不同的结果样式,所以我添加了一个进一步的条件。

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

<?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 \'post\':
            echo "<div class=\\"postsearch container\\"><h2>Blog Results</h2>";
            break;
        case \'product\':
            echo "<div class=\\"productsearch container\\"><h2>Product Search   Results</h2>";
            break;

    }
} 
?>

    <?php if(\'post\' == get_post_type()) : ?>
     <li class="post"><?php the_title(); ?></li>
<?php endif; ?>

  <?php if(\'product\' == get_post_type()) : ?>
 <li class="product"><?php the_title(); ?></li>
  <?php endif; ?>


<?php endwhile; ?>

<?php else : ?>
<div class="open-a-div">
<p>No results found.</p>    

<?php endif; ?>       

</div><!-- throw a closing div in --> 
基本上,唯一的变化是过滤器和结束div,它们在最后被省略了。还有“未找到结果”这是我的网站上的工作。

结束