按分类过滤自定义帖子类型

时间:2016-03-21 12:39:24

标签: php wordpress filter custom-post-type taxonomy

我想通过分类法过滤我的自定义帖子类型Job。我有两个分类法departmentlocation。我发布我的工作代码只是我的第一个分类。如何修改它以便它也适用于第二个分类法?

<?php
$term = isset($_GET['department']) ? sanitize_text_field($_GET['department']) : false;
$selected = '';
$tax_query = '';
if ($term) {
    $term = get_term_by('slug', $term, 'department');
    if (!empty($term->name)) {
        $selected = $term->name;
        $tax_query = array(
            array(
                'taxonomy' => 'department',
                'terms' => $term,
            )
        );
    }
} 
?>

<form id="tool-category-select" class="tool-category-select" action="<?php the_permalink(); ?>" method="get">
    <?php
    wp_dropdown_categories(
        array(
            'orderby' => 'NAME',        
            'taxonomy' => 'department',        
            'name' => 'department',       
            'value_field' => 'slug',        
            'show_option_all' => 'Departments',  
            'selected' => $selected,      
        )
    );
    ?>
    <input type="submit" name="submit" value="view"/>
</form>

0 个答案:

没有答案