wp-admin按分类法和分类法关系过滤cpt

时间:2019-11-14 14:24:44

标签: wordpress advanced-custom-fields custom-post-type wp-admin

我有“类别”和“消息”自定义帖子类型,它们都可以有一个名为“子类别”的自定义分类法。

我希望能够通过wp-admin上的自定义分类来过滤邮件,例如显示Subcategory_1中的所有邮件。我设法用发现的here代码来做到这一点。

问题是我还想按与邮件具有相同子类别的类别过滤它们。

示例:

  • 类别_1具有子类别_1
  • Category_2具有Subcategory_2
  • 消息_1具有子类别_1
  • 消息_2具有子类别_1
  • Message_3具有Subcategory_2

预期结果是,当用户从过滤器下拉列表中选择类别_1 时,仅显示 Message_1 Message_2

当前代码(只能按子类别过滤):

function todo_restrict_manage_posts() {
 global $typenow;
 $args=array( 'public' => true, '_builtin' => false ); 
 $post_types = get_post_types($args);
 if ( in_array($typenow, $post_types) ) {
 $filters = get_object_taxonomies($typenow);
    foreach ($filters as $tax_slug) {
        $tax_obj = get_taxonomy($tax_slug);
        wp_dropdown_categories(array(
            'show_option_all' => __('Show All '.$tax_obj->label ),
            'taxonomy' => $tax_slug,
            'name' => $tax_obj->name,
            'orderby' => 'term_order',
            'selected' => $_GET[$tax_obj->query_var],
            'hierarchical' => $tax_obj->hierarchical,
            'show_count' => true,
            'hide_empty' => false
        ));
    }
 }
}
function todo_convert_restrict($query) {
 global $pagenow;
 global $typenow;
 if ($pagenow=='edit.php') {
    $filters = get_object_taxonomies($typenow);
    foreach ($filters as $tax_slug) {
        $var = &$query->query_vars[$tax_slug];
        if ( isset($var) ) {
            $term = get_term_by('id',$var,$tax_slug);
            $var = $term->slug;
        }
    }
 }
 return $query;
}
 add_action( 'restrict_manage_posts', 'todo_restrict_manage_posts' );
 add_filter('parse_query','todo_convert_restrict');

当前过滤器(缺少按类别过滤的过滤器)

Current filters (filter by Category missing)

2 个答案:

答案 0 :(得分:1)

如果我对您的理解正确,并且假设类别帖子类型子句为categories,而子类别分类子句为subcategories,则以下应该为您工作:

SNIPPET 1:这将添加“按类别过滤”下拉菜单:

add_action( 'restrict_manage_posts', 'add_categories_cpt_filter' );
function add_categories_cpt_filter( $post_type ) {
    // Display the filter only on the Messages edit page.
    if ( 'messages' === $post_type ) {
        $posts = get_posts( [
            'post_type'      => 'categories',
            'posts_per_page' => 99999,
            'orderby'        => 'title',
            'order'          => 'ASC',
        ] );

        echo '<select name="categories_cpt" class="postform">';

        if ( ! empty( $posts ) ) {
            echo '<option value="0">Show All Categories</option>';
        } else {
            echo '<option value="0">No Categories</option>';
        }

        $selected = isset( $_GET['categories_cpt'] ) ?
            absint( $_GET['categories_cpt'] ) : 0;
        foreach ( $posts as $p ) {
            printf( '<option value="%s"%s>%s</option>',
                $p->ID, selected( $selected, $p->ID, false ),
                esc_html( $p->post_title ) );
        }

        echo '</select>';
    }
}

SNIPPET 2:这将按所选类别的条款过滤邮件:

add_action( 'pre_get_posts', 'filter_messages_by_categories_cpt' );
function filter_messages_by_categories_cpt( $query ) {
    // Enable the filter only on the Messages edit page.
    if ( $query->is_admin && $query->is_main_query() &&
        ( $screen = get_current_screen() ) &&
        ( 'edit-messages' === $screen->id ) &&
        ! empty( $_GET['categories_cpt'] )
    ) {
        $tax_query = $query->get( 'tax_query' );
        $tax_query = is_array( $tax_query ) ? $tax_query : [];

        $term_ids = get_terms( [
            'taxonomy'   => 'subcategories',
            'object_ids' => absint( $_GET['categories_cpt'] ),
            'fields'     => 'ids',
        ] );
        if ( ! is_wp_error( $term_ids ) && ! empty( $term_ids ) ) {
            $tax_query[] = [
                'taxonomy' => 'subcategories',
                'terms'    => $term_ids,
            ];

            $query->set( 'tax_query', $tax_query );
        }
    }
}

您可以在相关代码之后(即add_filter('parse_query','todo_convert_restrict');之后)添加以上两个代码段。

答案 1 :(得分:0)

问题是这一行:

$var = &$query->query_vars[$tax_slug];

&符号不是必需的,并且$ var现在是术语的子弹,而不是ID

所以这行:

$term = get_term_by('id',$var,$tax_slug);

需要为:

$term = get_term_by('slug',$var,$tax_slug);

让我知道它是否有效。