2017-05-04 138 views
1

我有一個名爲'藝術家'的自定義帖子類型。 我當前的搜索結果正在將此帖子類型與我的標準帖子一起拉入一個分組中。 我在分離結果中的兩個帖子類型時遇到了問題。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-## --> 

讚賞任何幫助。我意識到已經有一些類似的問題,但大多數對這種特殊情況沒有幫助。

回答

-1

只要改變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; 
} 
相關問題