# JSON Feed for Your Posts

> Publishes your latest posts as a JSON Feed at /feed/json/, a modern alternative to RSS supported by many feed readers and easy to use from apps and scripts.

- Type: PHP
- Runs: Run everywhere
- Category: [RSS Feeds](https://wpide.com/library/category/rss/)
- Tags: rss, json, feeds
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/json-feed/

## Code

```php
add_action( 'init', function () {
    add_feed( 'json', function () {
        $query = new WP_Query( array(
            'posts_per_page'      => (int) get_option( 'posts_per_rss', 10 ),
            'post_status'         => 'publish',
            'ignore_sticky_posts' => true,
            'has_password'        => false,
            'no_found_rows'       => true,
        ) );

        $items = array();

        while ( $query->have_posts() ) {
            $query->the_post();
            $post = get_post();

            $item = array(
                'id'             => get_permalink( $post ),
                'url'            => get_permalink( $post ),
                'title'          => html_entity_decode( get_the_title( $post ), ENT_QUOTES, 'UTF-8' ),
                'content_html'   => apply_filters( 'the_content', get_the_content() ),
                'summary'        => html_entity_decode( wp_strip_all_tags( get_the_excerpt() ), ENT_QUOTES, 'UTF-8' ),
                'date_published' => get_post_time( 'c', true, $post ),
                'date_modified'  => get_post_modified_time( 'c', true, $post ),
                'authors'        => array(
                    array(
                        'name' => get_the_author_meta( 'display_name', $post->post_author ),
                        'url'  => get_author_posts_url( $post->post_author ),
                    ),
                ),
            );

            $tags = get_the_tags( $post->ID );

            if ( $tags && ! is_wp_error( $tags ) ) {
                $item['tags'] = wp_list_pluck( $tags, 'name' );
            }

            if ( has_post_thumbnail( $post ) ) {
                $item['image'] = get_the_post_thumbnail_url( $post, 'large' );
            }

            $items[] = $item;
        }

        wp_reset_postdata();

        $feed = array(
            'version'       => 'https://jsonfeed.org/version/1.1',
            'title'         => get_bloginfo( 'name' ),
            'home_page_url' => home_url( '/' ),
            'feed_url'      => get_feed_link( 'json' ),
            'description'   => get_bloginfo( 'description' ),
            'language'      => get_bloginfo( 'language' ),
            'items'         => $items,
        );

        if ( has_site_icon() ) {
            $feed['icon'] = get_site_icon_url( 512 );
        }

        if ( ! headers_sent() ) {
            header( 'Content-Type: application/feed+json; charset=' . get_option( 'blog_charset' ) );
        }

        echo wp_json_encode( $feed, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT );
    } );
} );

add_action( 'wp_head', function () {
    echo '<link rel="alternate" type="application/feed+json" title="' . esc_attr( get_bloginfo( 'name' ) . ' JSON Feed' ) . '" href="' . esc_url( get_feed_link( 'json' ) ) . '" />' . "\n";
} );
```

## Notes

With pretty permalinks, visit Settings > Permalinks once after activating so the /feed/json/ address works; ?feed=json works right away.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "JSON Feed for Your Posts" 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/
