WP JSON list all permalinks

Is it possible to do this with a rest query + filters? eg.
http://wpsite.com/wp-json/wp/v2/posts?filter[only-permalinks]

Yes, since 4.9.8 (see #43874) it’s possible to render only fields needed with the _fields parameter.

Examples

Render only the permalinks:

https://example.com/wp-json/wp/v2/posts?_fields=link

[ 
    {
        link: "https://example.com/foo/"
    },
    {
        link: "https://example.com/bar/"
    },
]

Render only the post IDs and permalinks:

https://example.com/wp-json/wp/v2/posts?_fields=id,link

[ 
    {
        id: 123,
        link: "https://example.com/foo/"
    },
    {
        id: 234,
        link: "https://example.com/bar/"
    },
]

Render only the post IDs and the slugs:

https://example.com/wp-json/wp/v2/posts?_fields=id,slug

[ 
    {
        id: 123,
        slug: "foo"
    },
    {
        id: 234,
        slug: "bar"
    },
]

Fetching All Available Items

Currently we have to fetch all available items with paging, like:

https://example.comwp-json/wp/v2/posts?_fields=slug&per_page=100&page=1
https://example.comwp-json/wp/v2/posts?_fields=slug&per_page=100&page=2
...

where per_page is 10 by default and with maximum value of 100.

Fetching all items in a single request can result in fatal errors if the number of items exceeds the available resources.

The ticket #43998 to allow unbound requests (something like per_page=-1) for logged in users, was closed as wontfix because it doesn’t scale and has performance issues.

There’s a ticket #45140 to increase the upper bound for per_page to few hundreds.

Usually there’s no need to display so many items in a user interface and there exists javascript techniques to handle the pagination.

If more is really needed then one could filter the rest query via rest_{$post_type}_query to override the maximum default value of posts_per_page, at the risk of fatal errors if available resources are exceeded.

Leave a Comment