# Book Structured Data for a Books Post Type

> Adds Book JSON-LD with author, ISBN, publisher, page count and publication date to single book pages.

- Type: PHP
- Runs: Frontend only
- Category: [SEO](https://wpide.com/library/category/seo/)
- Tags: schema, books, json-ld
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/seo-book-schema/

## Code

```php
add_action( 'wp_head', function () {
    if ( ! is_singular( 'book' ) ) {
        return;
    }

    $post = get_queried_object();
    $get  = function ( $key ) use ( $post ) {
        return trim( (string) get_post_meta( $post->ID, '_book_' . $key, true ) );
    };

    $data = array(
        '@context'      => 'https://schema.org',
        '@type'         => 'Book',
        'name'          => wp_strip_all_tags( get_the_title( $post ) ),
        'description'   => wp_strip_all_tags( get_the_excerpt( $post ) ),
        'url'           => get_permalink( $post ),
        'image'         => get_the_post_thumbnail_url( $post, 'full' ),
        'isbn'          => $get( 'isbn' ),
        'numberOfPages' => absint( $get( 'pages' ) ),
        'datePublished' => $get( 'published' ),
    );

    if ( $get( 'author_name' ) ) {
        $data['author'] = array( '@type' => 'Person', 'name' => $get( 'author_name' ) );
    }

    if ( $get( 'publisher' ) ) {
        $data['publisher'] = array( '@type' => 'Organization', 'name' => $get( 'publisher' ) );
    }

    echo '<script type="application/ld+json">' . wp_json_encode( array_filter( $data ), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG ) . "</script>\n";
} );
```

## Notes

Reads the Book Details meta box fields.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Book Structured Data for a Books Post Type" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
