2

我在 WordPress 中创建了名为“job_listing”的自定义帖子。

所有帖子都存储在“job_listing”下,对于工作,我们有工作类型的信息,例如。永久,兼职等

此 job_type 存储在术语中,我想按 job_type 搜索和排序所有工作/帖子。

任何人有解决方案?

4

1 回答 1

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;
于 2012-11-22T12:50:37.563 回答