您如何使用自定义分类查询进行过滤?

How do you filter using a custom taxonomy query?

我正在尝试使用 ajax 通过我的自定义 post 按类别过滤,但它似乎不适用于自定义 post。

我创建了一个名为 'image_category' 的自定义分类法。

我认为我的路线是正确的:

$category = $_POST['category'];

$args = array(
              'post_type' => 'image',
              'posts_per_page' => -1,
              'tax_query' => array(
                 array( 'taxonomy' => 'image_category',
                 'field' => 'term_id',
                ),

           );

        if(isset($category)) {
            $args[2][1] = array($category);
                
}
  
$query = new WP_Query($args);
        
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();

  echo '<div class="col-md-3">hi</div>';

endwhile;
endif;
      

以下是我如何添加我的 image_category 自定义 post 类型:

function image_category() {

$labels = array(
    'name'                       => _x( 'Image Categories', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'Image Category', 'Taxonomy Singular Name', 'text_domain' ), 
    'menu_name'                  => __( 'Image Category', 'text_domain' ),
    'all_items'                  => __( 'All categories', 'text_domain' ),
    'parent_item'                => __( 'Parent Categories', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent Category:', 'text_domain' ),
    'new_item_name'              => __( 'New Category Name', 'text_domain' ),
    'add_new_item'               => __( 'Add New Category', 'text_domain' ),
    'edit_item'                  => __( 'Edit Category', 'text_domain' ),
    'update_item'                => __( 'Update Category', 'text_domain' ),
    'view_item'                  => __( 'View Category', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate categories with commas', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove categories', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
    'popular_items'              => __( 'Popular Categories', 'text_domain' ),
    'search_items'               => __( 'Search Categories', 'text_domain' ),
    'not_found'                  => __( 'Not Found', 'text_domain' ),
    'no_terms'                   => __( 'No categories', 'text_domain' ),
    'items_list'                 => __( 'Categories list', 'text_domain' ),
    'items_list_navigation'      => __( 'Categories list navigation', 'text_domain' ),
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
);
register_taxonomy( 'image_category', array( 'image' ), $args );

}
add_action( 'init', 'image_category', 0 );
$args = array(
              'post_type' => 'image',
              'posts_per_page' => -1,
              'tax_query' => array(
                 array( 'taxonomy' => 'image_category', 
                        'field' => 'term_id',
                       
                ),

           ),
       );

        if(isset($category)) {
           $args['tax_query'][0]['terms'] = array($category);
        }