WordPress Rest API query parameters mappings

This array defines mappings between public API query parameters whose values are accepted as-passed, and their internal WP_Query parameter name equivalents (some are the same). Only values which are also present in registered will be set.

Below is the list of all the endpoints which have query parameters mapping:

  • wp/v2/comments
  • wp/v2/<post-type>
  • wp/v2/<taxonomy>
  • wp/v2/<term>
  • wp/v2/<users>

Check below list of parameters mapping by Rest API endpoints:

wp/v2/comments

$parameter_mappings = array(
    'author'         => 'author__in',
    'author_email'   => 'author_email',
    'author_exclude' => 'author__not_in',
    'exclude'        => 'comment__not_in',
    'include'        => 'comment__in',
    'offset'         => 'offset',
    'order'          => 'order',
    'parent'         => 'parent__in',
    'parent_exclude' => 'parent__not_in',
    'per_page'       => 'number',
    'post'           => 'post__in',
    'search'         => 'search',
    'status'         => 'status',
    'type'           => 'type',
);

wp/v2/<post-type>

$parameter_mappings = array(
    'author'         => 'author__in',
    'author_exclude' => 'author__not_in',
    'exclude'        => 'post__not_in',
    'include'        => 'post__in',
    'menu_order'     => 'menu_order',
    'offset'         => 'offset',
    'order'          => 'order',
    'orderby'        => 'orderby',
    'page'           => 'paged',
    'parent'         => 'post_parent__in',
    'parent_exclude' => 'post_parent__not_in',
    'search'         => 's',
    'slug'           => 'post_name__in',
    'status'         => 'post_status',
);

wp/v2/<taxonomy>

$parameter_mappings = array(
    'exclude'    => 'exclude',
    'include'    => 'include',
    'order'      => 'order',
    'orderby'    => 'orderby',
    'post'       => 'post',
    'hide_empty' => 'hide_empty',
    'per_page'   => 'number',
    'search'     => 'search',
    'slug'       => 'slug',
);

wp/v2/<term>

       
$parameter_mappings = array(
    'exclude'    => 'exclude',
    'include'    => 'include',
    'order'      => 'order',
    'orderby'    => 'orderby',
    'post'       => 'post',
    'hide_empty' => 'hide_empty',
    'per_page'   => 'number',
    'search'     => 'search',
    'slug'       => 'slug',
);

wp/v2/<users>

$parameter_mappings = array(
    'exclude'  => 'exclude',
    'include'  => 'include',
    'order'    => 'order',
    'per_page' => 'number',
    'search'   => 'search',
    'roles'    => 'role__in',
    'slug'     => 'nicename__in',
);
Advertisements

WordPress Rest API Get specific posts by IDs by using `include` parameter

With the Rest API we get all the posts. But, Sometimes we want to get only specific posts by post ids. WordPress have rest API parameter include which return only requested posts by post ids.

Syntax:
https://example.org/wp-json/wp/v2/posts?include=<post-ids&gt;

Example 1:
https://example.org/wp-json/wp/v2/posts?include=11,12
It return only 2 posts whose ids are 11 & 12.

Note: By default it return 10 posts. If you have more than 10 posts then add parameter `per_page` which return all the posts.

Example 2:
https://example.org/wp-json/wp/v2/posts/?include=18177,18178,18198,18258,18137,18138,18143,18144,18167,18166,18314,18322,18131,18135,18145,18156,18159,18158,18165,18169,18183,18185,18199,18227,18253,18262,18263,18283,18312,18325,18330,18329,18332,18333,18140,18152,18163,18180,18193,18191,18195,18200,18204,18205,18214,18218,18219,18221,18224,18242,18252,18273,18286,18288,18307&amp;per_page=100

Add post meta in post (custom post type) endpoint with Rest API

Add post meta in post (custom post type) endpoint with Rest API.

Todo:

  • Change post-type with your post type slug.
  • Change prefix-meta-key with your post meta key which you want to include in Rest API Response.

Output:

Visit https://<mysite>/wp-json/wp/v2/<post-type>/ it show the stored data from meta key  prefix-meta-key in the Rest API response.

Code: