Add a searchable book catalogue to WordPress
Published Updated 2 min read
An astronomical society, a reading group, a community centre, a school department: plenty of organisations have a shelf of books they want visible on their website. The need is always the same: titles with author, year and subject, and a way for visitors to search or filter them.
The plugins that exist for it are overbuilt. Library Management System, Book Review Library and Books Gallery ship borrowing workflows, member accounts, loan periods and overdue notices. An astronomical society listing forty books about telescopes does not need a fines system.
A plugin that only displays books is small: a custom post type, two meta fields, a subject taxonomy and a shortcode that renders a filterable table. Around eighty lines in a single PHP file.
Books as posts, subjects as tags
Books get their own section in the admin menu. Each book is a post: the title field holds the title, and a meta box under the editor holds Author and Year Published. Subjects are a flat taxonomy that works like tags. Type a new subject while saving a book and it exists from then on, ready for the next one.
Adding a book is adding a post: title, author, year, tick or type the subjects, save. It appears in the catalogue immediately. There is no catalogue settings screen.
Search by title, filter by subject
[book_catalogue] on any page renders a search box above a table of every book, sorted alphabetically by title. Visitors can type a keyword to match titles, click a subject to filter by it, or do both, with the keyword narrowing within the chosen subject. When nothing matches, a short message says so.
The subject links are built from the terms actually attached to books, so a subject drops off the list once its last book is removed. To give a subject its own page, use [book_catalogue subject="astronomy"] with the term's slug, which you will find under Subjects > Edit. The shortcode works on as many pages as you like.
A catalogue, not a lending library
There is no idea of a copy being on the shelf or checked out: no reservations, member accounts, due dates or overdue notices. If a visitor needs to know whether a book is available, this cannot tell them. For a real lending library, where members borrow books and staff track who has what, a full library management system is what you need. Its catalogue looks much the same; the machinery behind it does not.
Book data is whatever you type in. Nothing is fetched from an ISBN database and no covers are pulled in; that takes a plugin that talks to a third-party API.
A follow-up message can add a Publisher field with its own table column, pagination at 20 books a page with links underneath for a large collection, or a single-book page with a longer description.
What you get
Included
- Books custom post type with author and year meta fields and a subject taxonomy
- A [book_catalogue] shortcode that renders a searchable, filterable table
- Subject filter links generated automatically from the taxonomy terms in use
- Alphabetical sort with keyword search by title and optional subject filtering
Not included
- Loan tracking: no borrowing, returning, holds, due dates, or overdue notices
- Member or patron accounts of any kind
- ISBN lookup or automatic import of book metadata from an external database
- Cover image fetching from a feed or ISBN API
- Pagination: all books render on one page by default
Library Management System does the things on the right. This build covers the part most sites use.
Build it in Steem
The button opens the builder with this guide's prompt filled in, so you can change anything before it runs. Nothing is built, and nothing is charged, until you send it.
Read the prompt (449 words)
Build a WordPress plugin called Book Catalogue. Plugin Name: Book Catalogue. Description: Displays a searchable list of books from a custom post type with subject filtering. Single PHP file. No dependencies beyond WordPress core. Register a custom post type called `book` with: label Books, singular_name Book, menu_name Library, public true, has_archive false, supports array containing only title, show_in_menu true, rewrite slug books, show_ui true. Register a flat taxonomy called `book_subject` attached to `book` with: label Subjects, singular_name Subject, hierarchical false, public true, show_admin_column true, show_ui true, rewrite slug book-subject. Add a meta box with id `book_meta` and title Book Details to the book post type on add and edit screens. The meta box contains two fields: Author (text input, name book_author) and Year Published (number input, name book_year, min 1000 max 2099). Output a nonce field using wp_nonce_field with action book_meta_nonce. On save_post: verify the nonce with check_admin_referer('book_meta_nonce'), return early if doing_autosave or if current_user_can('edit_post',$post_id) is false, save book_author with sanitize_text_field, save book_year with absint only if the value is between 1000 and 2099 inclusive otherwise save 0. Register a shortcode `book_catalogue`. The shortcode function should: 1. Read $_GET['book_search'] with sanitize_text_field and $_GET['book_subject'] with sanitize_key. 2. Build $args for WP_Query: post_type book, posts_per_page -1, orderby title, order ASC. If book_search is not empty add s to the args. If book_subject is not empty add a tax_query: taxonomy book_subject, field slug, terms book_subject value. 3. Render a search form: method GET, action empty string, one text input named book_search with the current search value set via esc_attr, a submit button labelled Search. Include a hidden input for book_subject if it is currently set so the subject filter persists through a keyword search. 4. Render subject filter links: call get_terms with taxonomy book_subject, hide_empty true. Output an All link pointing to the current page URL without book_subject. For each term output a link pointing to the current page URL with book_subject set to the term slug, using esc_html for the term name and esc_url for the URL. Use add_query_arg to build the URLs. Wrap the links in a paragraph. 5. Run the query. If no posts found output a paragraph saying No books found. Otherwise output an HTML table with a thead row containing columns Title, Author, Year, Subjects. For each post output a tr with: post_title escaped with esc_html, book_author meta escaped with esc_html, book_year meta output only if it is greater than 0 escaped with esc_html, and the subject terms as a comma-separated list of term names using get_the_terms($post_id,'book_subject') escaped with esc_html per term, or a hyphen if none. 6. Use ob_start and return ob_get_clean. No inline JS. No external CSS. No AJAX. No database tables. No block registration.
Steem