2012-11-22 5 views
2

"job_listing"이라는 이름의 WordPress에 맞춤 게시물을 만들었습니다.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

이 기능이 작동하기 때문에 upvoted되었습니다. 그러나 더 좋은 방법이 있어야합니다. 이 방법은 $ terms 배열을 반복 할 때마다 db에 대한 새로운 호출을 만든다. 게시물을 쿼리하고 택 소미로 정렬하고 PHP를 사용하여 배열을 용어로 분리하는 방법이 없습니까? – codescribblr

+1

트릭을 수행하고 조인을 사용하여 더 빠를 사용자 지정 SELECT 쿼리가있을 것입니다. 현실적으로는, 많은 용어를 사용하지 않는다면 아마도 큰 차이는 없을 것입니다. –

관련 문제