Lấy bài viết, custom post type từ category, post_type, custom taxonomy phần 1 (ok)

// ===========
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);
}
function grouped_by_taxonomy_main_query($query) {
  if ($query->is_home() && $query->is_main_query()) {
    // Run only on the homepage
    $post_ids = array();
    $terms    = get_terms('genre');
    foreach ($terms as $term) {
      $post_ids = array_merge($post_ids, get_posts(
        array(
          'posts_per_page' => 12, // as you wish...
          'tax_query'      => array(
            array(
              'taxonomy' => $term->taxonomy
            ),
          ),
        )
      ) // getting posts in the current term
      );
    }
    $query->query_vars['post_type']           = array('book', 'post'); // Again, if needed... Default is posts
    $query->query_vars['posts_per_page']      = 16; // If needed...
    $query->query_vars['post__in']            = $post_ids; // Filtering with the post ids we've obtained above
    $query->query_vars['orderby']             = 'post__in'; // Here we keep the order we generated in the terms loop
    $query->query_vars['ignore_sticky_posts'] = 1; // If you dont want your sticky posts to change the order
  }
}
// Hook my above function to the pre_get_posts action
add_action('pre_get_posts', 'grouped_by_taxonomy_main_query');
// ===========
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);
}
function grouped_by_taxonomy_main_query($query) {
  if ($query->is_home() && $query->is_main_query()) {
    // Run only on the homepage
    $post_ids = array();
    $terms    = get_terms('genre');
    foreach ($terms as $term) {
      $post_ids = array_merge($post_ids, get_posts(
        array(
          'posts_per_page' => 12, // as you wish...
          'tax_query'      => array(
            array(
              'taxonomy' => $term->taxonomy
            ),
          ),
        )
      ) // getting posts in the current term
      );
    }
    $query->query_vars['post_type']           = array('book', 'post'); // Again, if needed... Default is posts
    $query->query_vars['posts_per_page']      = 16; // If needed...
    $query->query_vars['post__in']            = $post_ids; // Filtering with the post ids we've obtained above
    $query->query_vars['orderby']             = 'post__in'; // Here we keep the order we generated in the terms loop
    $query->query_vars['ignore_sticky_posts'] = 1; // If you dont want your sticky posts to change the order
  }
}
// Hook my above function to the pre_get_posts action
add_action('pre_get_posts', 'grouped_by_taxonomy_main_query');

Last updated