function create_books_post_type() { // Set labels $labels = array( 'name' => 'Books', 'singular_name' => 'Book', 'add_new' => 'Add New', 'add_new_item' => 'Add New Book', 'edit_item' => 'Edit Book', 'new_item' => 'New Book', 'all_items' => 'All Books', 'view_item' => 'View Book', 'search_items' => 'Search Books', 'not_found' => 'No books found', 'not_found_in_trash' => 'No books found in Trash', 'parent_item_colon' => '', 'menu_name' => 'Books' ); // Set other options $args = array( 'labels' => $labels, 'public' => true, 'has_archive' => true, 'menu_icon' => 'dashicons-book-alt', 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'comments'), 'rewrite' => array('slug' => 'books'), ); // Register post type register_post_type( 'books', $args ); } add_action( 'init', 'create_books_post_type' );
$books = get_posts(array( 'post_type' => 'books', 'post_status' => 'publish', 'posts_per_page' => -1 )); foreach($books as $book){ echo $book->post_title . 'In this example, all the published books are retrieved and their post titles are printed using a loop. Package Library: This code example is using the built-in WordPress functions to create and retrieve custom post types. No external package library is used.
'; }