2015-09-07 49 views
1

我可以查询和显示所有自定义帖子类型,但是当尝试根据分类来查询它们时,它将不起作用。有人请帮忙!ACF使用分类法查询自定义帖子类型

分类功能

add_action('init', 'create_project_type_taxonomies', 0); 

function create_project_type_taxonomies() { 
// Add new taxonomy, make it hierarchical (like categories) 
$labels = array(
    'name'    => _x('Project Types', 'taxonomy general name'), 
    'singular_name'  => _x('Project Type', 'taxonomy singular name'), 
    'search_items'  => __('Search Project Types'), 
    'all_items'   => __('All Project Types'), 
    'parent_item'  => __('Parent Project Type'), 
    'parent_item_colon' => __('Parent Project Type:'), 
    'edit_item'   => __('Edit Project Type'), 
    'update_item'  => __('Update Project Type'), 
    'add_new_item'  => __('Add New Project Type'), 
    'new_item_name'  => __('New Project Type'), 
    'menu_name'   => __('Project Type'), 
); 

$args = array(
    'hierarchical'  => true, 
    'labels'   => $labels, 
    'show_ui'   => true, 
    'show_admin_column' => true, 
    'query_var'   => true, 
    'rewrite'   => array('slug' => 'project_type'), 
); 

register_taxonomy('project_type', array('project'), $args); 
} 

查询 - 这没有分类行工作

<?php 
     $projects = new WP_Query(
      array(
       'post_type' => 'project', 
       'project_type' => 'music_videos', 
      ) 
     ); 
     ?> 

     <?php if($projects->have_posts()) : ?> 
     <?php while($projects->have_posts()) : $projects->the_post(); ?> 

       <!--some query stuff which works--> 

       <?php endwhile; ?> 
    <?php endif; wp_reset_query(); ?> 

回答

0

你必须使用tax_query参数,如描述here

$args = array(
    'post_type' => 'project', 
    'tax_query' => array(
     array(
      'taxonomy' => 'project_type', 
      'field' => 'slug', 
      'terms' => 'music_videos', 
     ), 
    ), 
); 
$projects = new WP_Query($args); 

希望它有帮助:)

+0

谢谢这工作:) – JonnyBoggon

相关问题