过滤get_terms以仅返回具有已发布帖子和私人帖子的条款

时间:2018-09-10 07:27:38

标签: wordpress

我在get_terms过滤器中找到了一个同时过滤公共和私人帖子的过滤器

     <?php
        function get_terms_filter( $terms, $taxonomies, $args )
{
    global $wpdb,$typenow;
    if($typenow == "product"){
        $taxonomy = $taxonomies[0];
        if ( ! is_array($terms) && count($terms) < 1 )
            return $terms;
        $filtered_terms = array();
        foreach ( $terms as $term )
        {
            $result = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts p JOIN $wpdb->term_relationships rl ON p.ID = rl.object_id WHERE rl.term_taxonomy_id = $term->term_id AND p.post_status IN ('publish','private') LIMIT 1");
            $term->count = $result;
            if ( intval($result) > 0 )
                $filtered_terms[] = $term;
        }
        return $filtered_terms;
    }
    else{
        $taxonomy = $taxonomies[0];
        if ( ! is_array($terms) && count($terms) < 1 )
            return $terms;
        $filtered_terms = array();
        foreach ( $terms as $term )
        {
            $result = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts p JOIN $wpdb->term_relationships rl ON p.ID = rl.object_id WHERE rl.term_taxonomy_id = $term->term_id AND p.post_status IN ('publish') LIMIT 1");


                $filtered_terms[] = $term;
        }
        return $filtered_terms;
    }
}
        add_filter('get_terms', 'get_terms_filter', 10, 3);
        ?>

0 个答案:

没有答案