ChatGPT(GPTS)のAPIからワードプレスの関数を呼び出して結果を取り込むサンプルコードをご紹介いたします。

ChatGPTが最新のワードプレスの投稿のタイトルを取得するサンプルコード

GPTSのAPI(アクション)に次のアクションを設定します。

openapi: 3.1.0
info:
  title: WordPress Latest Post API
  version: 1.0.0
servers:
  - url: https://example.com #ワードプレスのURL
paths:
  /wp-json/wp/v2/posts:
    get:
      operationId: getLatestPost
      summary: Get the latest WordPress post
      description: Fetch the most recent single WordPress post in JSON format.
      parameters:
        - name: per_page
          in: query
          required: true
          schema:
            type: integer
            enum: [1] #1件のみ取得
          example: 1 #1件のみ取得
      responses:
        '200':
          description: Latest post retrieved
          content:
            application/json:
              schema:
                type: array
                items:
                  type: object
                  properties:
                    id:
                      type: integer
                    date:
                      type: string
                      format: date-time
                    slug:
                      type: string
                    link:
                      type: string
                      format: uri
                    title:
                      type: object
                      properties:
                        rendered:
                          type: string
                    excerpt:
                      type: object
                      properties:
                        rendered:
                          type: string
                    content:
                      type: object
                      properties:
                        rendered:
                          type: string

ワードプレスは下記のようなURLを叩くと、最新の記事の投稿データをJSON形式で返します。

http://ワードプレスのURL/wp-json/wp/v2/posts?per_page=1

これをCHATGPTに実行させ結果のデータをCHATGPTに読み込ませるアクションとなります。

上記データからタイトルだけを出力させるインストラクション(指示)

この指示は下記のようになります。

[getLatestPost] を呼び出して、取得した記事のタイトルだけを表示

上記指示をGPTSの指示に書き込むとワードプレスの最新の記事をアクションgetLatestPostを実行してリアルタイムで引っ張ってきてくれて、表示してくれます。

ご参考になりましたら幸いです。

WordPress ワードプレスとCHATGPTの連携のご依頼・ご相談はWPドクターまでお気軽にお送りください