# Lấy bài viết, custom post type từ category, post\_type, custom taxonomy phần 2 (ok)

C:\xampp\htdocs\wordpress\wp-content\themes\twentytwentyone\single.php

```
$args = array(
	'post_type' => 'book',  
  'tax_query' => array(                     //(array) - Lấy bài viết dựa theo taxonomy
      array(
        'taxonomy' => 'genre',                //(string) - Tên của taxonomy
        'field' => 'id',                    //(string) - Loại field cần xác định term của taxonomy, sử dụng 'id' hoặc 'slug'
        'terms' => array( '15' ),    //(int/string/array) - Slug của các terms bên trong taxonomy cần lấy bài
        'include_children' => false,           //(bool) - Lấy category con, true hoặc false
        'operator' => 'IN'                    //(string) - Toán tử áp dụng cho mảng tham số này. Sử dụng 'IN' hoặc 'NOT IN'
      )
    ),
);
$query = new WP_Query($args);
echo '<pre>';
var_export($query);
echo '</pre>';
```

C:\xampp\htdocs\wordpress\wp-content\themes\twentytwentyone\functions.php

```
// ===========
add_action('init', 'my_book_cpt');
function my_book_cpt() {
  $labels = array(
    'name'               => _x('Books', 'post type general name', 'your-plugin-textdomain'),
    'singular_name'      => _x('Book', 'post type singular name', 'your-plugin-textdomain'),
    'menu_name'          => _x('Books', 'admin menu', 'your-plugin-textdomain'),
    'name_admin_bar'     => _x('Book', 'add new on admin bar', 'your-plugin-textdomain'),
    'add_new'            => _x('Add New', 'book', 'your-plugin-textdomain'),
    'add_new_item'       => __('Add New Book', 'your-plugin-textdomain'),
    'new_item'           => __('New Book', 'your-plugin-textdomain'),
    'edit_item'          => __('Edit Book', 'your-plugin-textdomain'),
    'view_item'          => __('View Book', 'your-plugin-textdomain'),
    'all_items'          => __('All Books', 'your-plugin-textdomain'),
    'search_items'       => __('Search Books', 'your-plugin-textdomain'),
    'parent_item_colon'  => __('Parent Books:', 'your-plugin-textdomain'),
    'not_found'          => __('No books found.', 'your-plugin-textdomain'),
    'not_found_in_trash' => __('No books found in Trash.', 'your-plugin-textdomain'),
  );
  $args = array(
    'labels'             => $labels,
    'description'        => __('Description.', 'your-plugin-textdomain'),
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,
    'query_var'          => true,
    'rewrite'            => array('slug' => 'book'),
    'capability_type'    => 'post',
    'has_archive'        => true,
    'hierarchical'       => false,
    'menu_position'      => null,
    'show_in_rest'       => true,
    'rest_base'          => 'books',
    'supports'           => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),
  );
  register_post_type('book', $args);
  flush_rewrite_rules(true);
}
// ===========
add_action('init', 'my_book_taxonomy', 30);
function my_book_taxonomy() {
  $labels = array(
    'name'              => _x('Genres', 'taxonomy general name'),
    'singular_name'     => _x('Genre', 'taxonomy singular name'),
    'search_items'      => __('Search Genres'),
    'all_items'         => __('All Genres'),
    'parent_item'       => __('Parent Genre'),
    'parent_item_colon' => __('Parent Genre:'),
    'edit_item'         => __('Edit Genre'),
    'update_item'       => __('Update Genre'),
    'add_new_item'      => __('Add New Genre'),
    'new_item_name'     => __('New Genre Name'),
    'menu_name'         => __('Genre'),
  );
  $args = array(
    'hierarchical'      => true,
    'labels'            => $labels,
    'show_ui'           => true,
    'show_admin_column' => true,
    'query_var'         => true,
    'rewrite'           => array('slug' => 'genre'),
    'show_in_rest'      => true,
    'rest_base'         => 'genre',
  );
  register_taxonomy('genre', array('book'), $args);
  flush_rewrite_rules(true);
}
```

![](https://1113999984-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M84NwP_L_qEsBHOa2xo%2F-MOjUnZaR36AVhwKrIGf%2F-MOjW80GP8v2Qdhjtqed%2FScreenshot_7.png?alt=media\&token=cf54e6ba-d71a-4299-a7d1-2a6a0ddb10ee)

![](https://1113999984-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M84NwP_L_qEsBHOa2xo%2F-MOjUnZaR36AVhwKrIGf%2F-MOjWAbP1YYZui-5Duez%2FScreenshot_8.png?alt=media\&token=6c804f64-6fdb-4e60-b12d-36c963ec88c2)

![](https://1113999984-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M84NwP_L_qEsBHOa2xo%2F-MOjUnZaR36AVhwKrIGf%2F-MOjWHPB6j0fX_TxaxVi%2FScreenshot_9.png?alt=media\&token=91d07371-ed43-458f-9ae7-97380af0ac00)

![](https://1113999984-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M84NwP_L_qEsBHOa2xo%2F-MOjUnZaR36AVhwKrIGf%2F-MOjWJaMcVWrQFhRIeB7%2FScreenshot_10.png?alt=media\&token=68fd9de6-f397-4e08-b4dd-f0cc0ac12b5d)

![](https://1113999984-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M84NwP_L_qEsBHOa2xo%2F-MOjUnZaR36AVhwKrIGf%2F-MOjWM1COB2N4Vm_GGTe%2FScreenshot_12.png?alt=media\&token=820b961b-7b3b-4f35-9d86-8727fd497636)
