Php 如何使用自定义分类查询进行筛选?
我试图使用ajax通过我的自定义帖子按类别进行过滤,但它似乎不适用于自定义帖子 我创建了一个名为“image\u category”的自定义分类法 我想我说得对:Php 如何使用自定义分类查询进行筛选?,php,custom-post-type,Php,Custom Post Type,我试图使用ajax通过我的自定义帖子按类别进行过滤,但它似乎不适用于自定义帖子 我创建了一个名为“image\u category”的自定义分类法 我想我说得对: $category = $_POST['category']; $args = array( 'post_type' => 'image', 'posts_per_page' => -1, 'tax_query' => array(
$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;
如果您需要我的ajax代码或自定义post函数,
$category
包含哪些内容,请告诉我?这是一个单值还是一个数组?一个单值@cbroe,我们在这里讨论的是默认的WP类别,而不是您专门为您的CPT创建的自定义分类法?$args['tax\u query'][0]['terms']
应该是您想要在这里设置的。
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);
}