WP REST APIでの戻り値を確認する(POST編)

以外と戻り値に関する記事がなかったので、覚書としてまとめてみました。 そのまま取得してみる アクセスしたURL:https://vccw.dev/wp-json/wp/v2/posts?filter[order]=ASC […]

広告ここから
広告ここまで

目次

    以外と戻り値に関する記事がなかったので、覚書としてまとめてみました。

    そのまま取得してみる

    アクセスしたURL:https://vccw.dev/wp-json/wp/v2/posts?filter[order]=ASC&filter[posts_per_page]=1
    *VCCWのデフォルトURLを使ってます。適当に開発環境とかに書き換えてみてください。

    [js]
    [{"id":1000,"date":"2009-05-15T14:48:32","date_gmt":"2009-05-15T21:48:32","guid":{"rendered":"http:\/\/wptest.io\/demo\/?p=1000"},"modified":"2009-05-15T14:48:32","modified_gmt":"2009-05-15T21:48:32","slug":"edge-case-nested-and-mixed-lists","type":"post","link":"http:\/\/wp-theme.dev\/2009\/05\/15\/edge-case-nested-and-mixed-lists\/","title":{"rendered":"Edge Case: Nested And Mixed Lists"},"content":{"rendered":"<p>Nested and mixed lists are an interesting beast. It&#8217;s a corner case to make sure that<\/p>\n<ul>\n<li><span style=\"line-height:1.714285714;font-size:1rem;\">Lists within lists do not break the ordered list numbering order<\/span><\/li>\n<li><span style=\"line-height:1.714285714;font-size:1rem;\">Your list styles go deep enough.<\/span><\/li>\n<\/ul>\n<h3>Ordered &#8211; Unordered &#8211; Ordered<\/h3>\n<ol>\n<li>ordered item<\/li>\n<li>ordered item\n<ul>\n<li><strong>unordered<\/strong><\/li>\n<li><strong>unordered<\/strong>\n<ol>\n<li>ordered item<\/li>\n<li>ordered item<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<\/li>\n<li>ordered item<\/li>\n<li>ordered item<\/li>\n<\/ol>\n<h3>Ordered &#8211; Unordered &#8211; Unordered<\/h3>\n<ol>\n<li>ordered item<\/li>\n<li>ordered item\n<ul>\n<li><strong>unordered<\/strong><\/li>\n<li><strong>unordered<\/strong>\n<ul>\n<li>unordered item<\/li>\n<li>unordered item<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>ordered item<\/li>\n<li>ordered item<\/li>\n<\/ol>\n<h3>Unordered &#8211; Ordered &#8211; Unordered<\/h3>\n<ul>\n<li>unordered item<\/li>\n<li>unordered item\n<ol>\n<li>ordered<\/li>\n<li>ordered\n<ul>\n<li>unordered item<\/li>\n<li>unordered item<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<\/li>\n<li>unordered item<\/li>\n<li>unordered item<\/li>\n<\/ul>\n<h3>Unordered &#8211; Unordered &#8211; Ordered<\/h3>\n<ul>\n<li>unordered item<\/li>\n<li>unordered item\n<ul>\n<li>unordered<\/li>\n<li>unordered\n<ol>\n<li><strong>ordered item<\/strong><\/li>\n<li><strong>ordered item<\/strong><\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<\/li>\n<li>unordered item<\/li>\n<li>unordered item<\/li>\n<\/ul>\n"},"excerpt":{"rendered":"<p>Nested and mixed lists are an interesting beast. It&#8217;s a corner case to make sure that Lists within lists do not break the ordered list numbering order Your list styles go deep enough. Ordered &#8211; Unordered &#8211; Ordered ordered item ordered item unordered unordered ordered item ordered item ordered item ordered item Ordered &#8211; Unordered [&hellip;]<\/p>\n"},"author":2,"featured_image":0,"comment_status":"closed","ping_status":"closed","sticky":false,"format":"standard","_links":{"self":[{"href":"http:\/\/wp-theme.dev\/wp-json\/wp\/v2\/posts\/1000"}],"collection":[{"href":"http:\/\/wp-theme.dev\/wp-json\/wp\/v2\/posts"}],"author":[{"embeddable":true,"href":"http:\/\/wp-theme.dev\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/wp-theme.dev\/wp-json\/wp\/v2\/comments?post=1000"}],"version-history":[{"href":"http:\/\/wp-theme.dev\/wp-json\/wp\/v2\/posts\/1000\/revisions"}],"https:\/\/api.w.org\/attachment":[{"href":"http:\/\/wp-theme.dev\/wp-json\/wp\/v2\/media?parent=1000"}],"https:\/\/api.w.org\/term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wp-theme.dev\/wp-json\/wp\/v2\/posts\/1000\/terms\/category"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wp-theme.dev\/wp-json\/wp\/v2\/posts\/1000\/terms\/tag"},{"taxonomy":"post_format","embeddable":true,"href":"http:\/\/wp-theme.dev\/wp-json\/wp\/v2\/posts\/1000\/terms\/post_format"}],"https:\/\/api.w.org\/meta":[{"embeddable":true,"href":"http:\/\/wp-theme.dev\/wp-json\/wp\/v2\/posts\/1000\/meta"}]}}]
    [/js]

    わかりにくいですね。

    jqを使ってキーだけとってみましょう

    確認に使ったコマンド

    [bash]
    $ curl -l https://vccw.dev/wp-json/wp/v2/posts | jq ".[0]" | jq "keys"
    [/bash]

    戻り値

    [bash]
    $ curl -l https://vccw.dev/wp-json/wp/v2/posts | jq ".[0]" | jq "keys"
    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed
    100 23239 0 23239 0 0 13018 0 –:–:– 0:00:01 –:–:– 13019
    [
    "_links", //関連リンク
    "author", //著者
    "comment_status", //コメントステータス
    "content", //本文
    "date", //投稿日
    "date_gmt", //投稿日(GMT)
    "excerpt", //概要
    "featured_image", //サムネイル画像
    "format", //投稿フォーマット
    "guid", //投稿URL
    "id", //投稿ID
    "link", //パーマリンク
    "modified", //更新日
    "modified_gmt", //更新日(GMT)
    "ping_status", //PINGステータス
    "slug", //投稿スラッグ
    "sticky", //先頭に固定
    "title", //タイトル
    "type" //投稿タイプ
    ]
    [/bash]
    一部ネストしてる値もありますが、おおよそこんなイメージです。
    「_link」はおそらく内部処理などで使うことを想定されている値かなと思います。

    上を踏まえてもう一度取得してみる

    [js]
    [
    {
    "_links": {
    "author": [
    {
    "embeddable": true,
    "href": "https://wp-theme.dev/wp-json/wp/v2/users/2"
    }
    ],
    "collection": [
    {
    "href": "https://wp-theme.dev/wp-json/wp/v2/posts"
    }
    ],
    "https://api.w.org/attachment": [
    {
    "href": "https://wp-theme.dev/wp-json/wp/v2/media?parent=1000"
    }
    ],
    "https://api.w.org/meta": [
    {
    "embeddable": true,
    "href": "https://wp-theme.dev/wp-json/wp/v2/posts/1000/meta"
    }
    ],
    "https://api.w.org/term": [
    {
    "embeddable": true,
    "href": "https://wp-theme.dev/wp-json/wp/v2/posts/1000/terms/category",
    "taxonomy": "category"
    },
    {
    "embeddable": true,
    "href": "https://wp-theme.dev/wp-json/wp/v2/posts/1000/terms/tag",
    "taxonomy": "post_tag"
    },
    {
    "embeddable": true,
    "href": "https://wp-theme.dev/wp-json/wp/v2/posts/1000/terms/post_format",
    "taxonomy": "post_format"
    }
    ],
    "replies": [
    {
    "embeddable": true,
    "href": "https://wp-theme.dev/wp-json/wp/v2/comments?post=1000"
    }
    ],
    "self": [
    {
    "href": "https://wp-theme.dev/wp-json/wp/v2/posts/1000"
    }
    ],
    "version-history": [
    {
    "href": "https://wp-theme.dev/wp-json/wp/v2/posts/1000/revisions"
    }
    ]
    },
    "author": 2,
    "comment_status": "closed",
    "content": {
    "rendered": "<p>Nested and mixed lists are an interesting beast. It&#8217;s a corner case to make sure that</p><ul><li><span style="line-height:1.714285714;font-size:1rem;">Lists within lists do not break the ordered list numbering order</span></li><li><span style="line-height:1.714285714;font-size:1rem;">Your list styles go deep enough.</span></li></ul><h3>Ordered &#8211; Unordered &#8211; Ordered</h3><ol><li>ordered item</li>

    "
    },
    "date": "2009-05-15T14:48:32",
    "date_gmt": "2009-05-15T21:48:32",
    "excerpt": {
    "rendered": "<p>Nested and mixed lists are an interesting beast. It&#8217;s a corner case to make sure that Lists within lists do not break the ordered list numbering order Your list styles go deep enough. Ordered &#8211; Unordered &#8211; Ordered ordered item ordered item unordered unordered ordered item ordered item ordered item ordered item Ordered &#8211; Unordered [&hellip;]</p><li>ordered item<ul><li><strong>unordered</strong></li><li><strong>unordered</strong><ol><li>ordered item</li><li>ordered item</li></ol></li></ul></li><li>ordered item</li><li>ordered item</li></ol><h3>Ordered &#8211; Unordered &#8211; Unordered</h3><ol><li>ordered item</li><li>ordered item<ul><li><strong>unordered</strong></li><li><strong>unordered</strong><ul><li>unordered item</li><li>unordered item</li></ul></li></ul></li><li>ordered item</li><li>ordered item</li></ol><h3>Unordered &#8211; Ordered &#8211; Unordered</h3><ul><li>unordered item</li><li>unordered item<ol><li>ordered</li><li>ordered<ul><li>unordered item</li><li>unordered item</li></ul></li></ol></li><li>unordered item</li><li>unordered item</li></ul><h3>Unordered &#8211; Unordered &#8211; Ordered</h3><ul><li>unordered item</li><li>unordered item<ul><li>unordered</li><li>unordered<ol><li><strong>ordered item</strong></li><li><strong>ordered item</strong></li></ol></li></ul></li><li>unordered item</li><li>unordered item</li></ul>"
    },
    "featured_image": 0,
    "format": "standard",
    "guid": {
    "rendered": "https://wptest.io/demo/?p=1000"
    },
    "id": 1000,
    "link": "https://wp-theme.dev/2009/05/15/edge-case-nested-and-mixed-lists/",
    "modified": "2009-05-15T14:48:32",
    "modified_gmt": "2009-05-15T21:48:32",
    "ping_status": "closed",
    "slug": "edge-case-nested-and-mixed-lists",
    "sticky": false,
    "title": {
    "rendered": "Edge Case: Nested And Mixed Lists"
    },
    "type": "post"
    }
    ]
    [/js]
    ・・・なんとなーくイメージつきましたかね?

    _linkに入っているURLは「この記事についたコメント」とか「この記事のカテゴリアーカイブ」とかのAPIになっているので、

    続けて何かのデータを取って来たい場合に活用することなりそうです。

    ということで次回はこれを使ってまたサンプルを作ってみます。

    広告ここから
    広告ここまで
    Home
    Search
    Bookmark