我在WordPress中创建了名为“job_listing”的自定义帖子。
所有帖子都存储在“job_listing”下,对于作业,我们有作业类型的信息,例如。永久,兼职等。
此job_type存储在术语中,我想按job_type搜索和排序所有作业/帖子。
任何人都有解决方案吗?
答案 0 :(得分: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;