2014-09-22 32 views
0

目前,我有以下代碼:Woocommerce - 產品展示在搜索結果一切

<?php while (have_posts()) : the_post(); ?> 

     <?php 
      if (get_post_type() !== 'post') { 
       if (get_post_type() == 'landing-pages') { 
        get_template_part('templates/content/archive', 'landingpages'); 
       } elseif (get_post_type() == 'product') { 
        get_template_part('templates/content/archive', 'product'); 
       } else { 
        get_template_part('templates/content/archive', 'cpt'); 
       } 
      } else { 
       get_template_part('templates/content/archive', 'posts'); 
      }; 
     ?>   

    <?php endwhile; ?> 

    <?php get_template_part('templates/modules/nav', 'pagination'); ?> 

然而,這只是顯示最新到最舊。隨着自定義帖子,woocommerce產品,帖子,頁面,所有混合togather。

我想只顯示產品,那麼一旦所有匹配的產品已顯示,則返回其他任何相匹配的搜索查詢,例如:其他自定義文章類型,頁面和職位。

就如何實現這一目標有什麼建議?

謝謝。

回答

0

複製並粘貼到你的主題的functions.php文件下面的代碼。

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

    if(!is_admin() && is_search()) 
     $orderby = $wpdb->prefix."posts.post_type DESC, {$wpdb->prefix}posts.post_date DESC"; 

    return $orderby; 
} 
+0

感謝不幸的是,「推薦」自定義帖子在產品之前會顯示在搜索結果中。我們需要先展示產品,然後再展示其他產品。從我做的快速測試中看起來是正確的。 – Martin 2014-09-22 05:40:36

+0

實際上,這並不意味着工作:在產品(woocommerce自定義帖子類型)中顯示感言(自定義帖子類型) – Martin 2014-09-22 05:47:27

相關問題