2012-11-22 54 views
2

我在WordPress中創建了名爲「job_listing」的自定義文章。如何按照wordpress中的自定義分類排序帖子?

所有的職位都存儲在「job_listing」下,而我們有工作類型的職位信息,例如。永久,兼職等

這job_type存儲在術語中,我想按job_type搜索和排序的所有職位/職位。

任何人都有解決方案?

+0

確認 - 'job_listing'是自定義帖子類型,'job_type'是自定義分類標準嗎? –

+3

yes.job_listing是一個自定義帖子類型,job_type是一個自定義分類標準 –

回答

4

嘗試類似這樣的事情。它將首先獲得job_type分類標準的所有非空項,然後遍歷它們,輸出相關的帖子。

我已經做出了這樣的假設,您希望它按字母順序(按作業類型和作業列表)進行排序,但您可以根據需要進行更改。

/** Get all terms from the 'job_type' Taxonomy */ 
$terms = get_terms('job_type', 'orderby=slug&hide_empty=1');  

/** Loop through each term one by one and output the posts that are assoiated to it */ 
if(!empty($terms)) : foreach($terms as $term) : 

     /** Set the term name */ 
     $term_name = $term->slug; 

     /** Set the query arguments and run the query*/ 
     $args = array(
      'job_type' => $term_name, 
      'orderby' => 'title', 
      'order' => ASC, 
      'post_type' => 'job_listing', 
      'posts_per_page' => -1 
     ); 
     $term_posts = new WP_Query($args); 

     /** Do something with all of the posts */ 
     if($term_posts->have_posts()) : while ($term_posts->have_posts()) : $term_posts->the_post(); 

       the_title(); 
       the_content(); 

      endwhile; 
     endif; 

     /** Reset the postdata, just because it's neat and tidy to do so, even if you don't need it again */ 
     wp_reset_postdata(); 

    endforeach; 
endif; 
+0

我投了票,因爲這個工作。但是,有一個更好的方法。每次循環$ terms數組時,這種方式都會對db進行新的調用。沒有辦法查詢這些帖子,按分類排序並使用php將數組分割成它的術語? – codescribblr

+1

我確定有一個自定義的SELECT查詢可以做到這一點,並且速度更快(使用連接)。實際上,除非你有大量的條款,否則它可能沒有什麼區別。 –

相關問題