WordPress的搜索结果分离自定义帖子类型的主要帖子

问题描述:

我有一个名为'艺术家'的自定义帖子类型。 我当前的搜索结果正在将此帖子类型与我的标准帖子一起拉入一个分组中。 我在分离结果中的两个帖子类型时遇到了问题。WordPress的搜索结果分离自定义帖子类型的主要帖子

而不是...

  • 博客文章
  • 博客文章
  • 艺术家发表
  • 博客文章
  • 艺术家发表

我想... 。

篇博客文章:

  • 博客文章
  • 博客文章
  • 博客文章

艺术家帖子:

  • 艺术家发表
  • 艺术家发表

这可能吗?我不确定我是否可以用一个循环/查询来解决这个问题,或者使用两个查询会更快?

目前的代码是在这里...

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 
    <header class="entry-header"> 
     <?php the_title(sprintf('<h1 class="entry-title"><a href="%s" rel="bookmark">', esc_url(get_permalink())), '</a></h1>'); ?> 

     <?php if ('post' == get_post_type()) : ?> 
     <div class="entry-meta"> 
      <?php the_date(); ?> 
     </div><!-- .entry-meta --> 
     <?php endif; ?> 
    </header><!-- .entry-header --> 

    <div class="entry-summary"> 
     <?php the_excerpt(); ?> 
    </div><!-- .entry-summary --> 
</article><!-- #post-## --> 

赞赏任何帮助。我意识到已经有一些类似的问题,但大多数对这种特殊情况没有帮助。

只要改变wp_query对象

$args = array(
    'order' => 'type' 
); 
$query = new WP_Query($args); 

OP是懒惰的,以使在这里阅读文档我提供了一个例子:

$args = array('order' => 'type'); 
$my_query = new WP_Query($args); 
while ($my_query->have_posts()) { 
// 
} 

编辑: 这里,如果你另一种可能的解决方案想要避免wp-query的新实例并使用全局对象:

add_filter('posts_orderby','my_sort_custom',10,2); 
function my_sort_custom($orderby, $query){ 
    global $wpdb; 

    if(!is_admin() && is_search()) 
     $orderby = $wpdb->prefix."posts.post_type ASC" 

    return $orderby; 
}