0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

WordPress REST API タグのslug名でデータを取得する

Posted at

概要

  • REST APIをつかったpostの取得の話です。
  • wordpress4.7以降は、セキュリティ上の都合で、それまでできていたタグのslug名での直接の絞り込みができなくなったそうな。参考リンク
  • いろいろ調べてうまくいった方法があった、かつ(多分)日本語でズバリの情報がなかったので、記録しておきます。もっといいやり方があれば、教えてください!

実装

  1. まずは、タグのidを突き止めます
  2. 取得したidを使ってfetch!以上!
  // 指定したslugでタグ(今回は"hoge")のid取得 
  let res = await fetch('https://sample.com/wp-json/wp/v2/tags?slug=hoge');
  let tags = await res.json();

  // 上記idをもとにアイテムを取得
  let tag = tags[0];
  if (tag) {
    let res = await fetch(`https://sample.com/wp-json/wp/v2/news?tags=${tag.id}`);
    let posts = await res.json();
  }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?