質問: array( 'post_type' => 'adverts', 'advert_tag' => 'politics' // doesn't seem to work. ); query_posts($args); while ( have_posts() ) : the_post(); //show posts endwhile; 分類法の宣言:add_action( 'init', 'add_custom_taxonomy', 0 ); function add_custom_taxonomy() { register_taxonomy('advert_tag', 'adverts', array( 'hierarchical' => true, 'labels' => array( 'name' => _x( 'advert tags', 'taxonomy general name' ), 'singular_name' => _x( 'advert tag', 'taxonomy singular name' ), 'search_items' => __( 'search advert tags' ), 'all_items' => __( 'all advert tags' ), 'parent_item' => __( 'parent advert tag' ), 'parent_item_colon' => __( 'parent advert tag:' ), 'edit_item' => __( 'edit advert tag' ), 'update_item' => __( 'update advert tag' ), 'add_new_item' => __( 'add new advert tag' ), 'new_item_name' => __( 'new advert tag name' ), 'menu_name' => __( 'advert tags' ), ), 'rewrite' => array( 'slug' => 'advert-tags', 'with_front' => false, 'hierarchical' => true ), )); } カスタム投稿タイプの宣言: add_action( 'init', 'create_post_type' ); function create_post_type() { register_post_type( 'adverts', array( 'labels' => array( 'name' => __( 'adverts' ), 'singular_name' => __( 'advert'), 'add_new' => __( 'add new' ), 'add_new_item' => __( 'add a new advert' ), 'edit' => __( 'edit' ), 'edit_item' => __( 'edit advert' ), 'new_item' => __( 'new advert' ), 'view' => __( 'view' ), 'view_item' => __( 'view advert' ), 'search_items' => __( 'search adverts' ), 'not_found' => __( 'no adverts found' ), 'not_found_in_trash' => __( 'no adverts found in trash' ), ), 'supports' => array( 'title', 'thumbnail', ), 'has_archive' => true, 'menu_position' => 10, 'public' => true, 'rewrite' => array( 'slug' => 'adverts' ), 'taxonomies' => array('advert_tag') ) ); }
回答: すべての最初の これまでquery_posts()を使ってはいけません 、詳しくはこちらをご覧ください: いつwp_query vs query_posts()vs get_posts()を使うべきですか? 。必要なものを投稿するにはwp_queryを使用する必要があります。それについては ドキュメント を読んでください。あなたの場合、クエリは次のようになります。$the_query = new wp_query( array( 'post_type' => 'adverts', 'tax_query' => array( array ( 'taxonomy' => 'advert_tag', 'field' => 'slug', 'terms' => 'politics', ) ), ) ); while ( $the_query->have_posts() ) : $the_query->the_post(); // show posts ... endwhile; /* restore original post data * nb: because we are using new wp_query we aren't stomping on the * original $wp_query and it does not need to be reset. */ wp_reset_postdata();