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?

LaravelからWordPressを呼び出すとI18nライブラリで干渉する時がある.

0
Last updated at Posted at 2026-07-12

LaravelからWordPressを簡単に呼び出しするにはwp-load.phpを呼び出せば使えるかと思いきや!?双方同じライブラリを使用していると干渉して処理しない場合があります(エラーを出力🤮).

その為にはDBから呼び出すしか他ない...(他にも方法はありますが)
LaravelからWordPressを呼び出すサービスを作ったので公開します.
尚、下記のコードはコンフィグディレクトリのdatabase.php設定を追加してあげないと動きません(設定方法は割愛します).

下記のサービスでは過去3か月の公開記事からランダムで取得するメソッド公開済みの新着記事を取得するメソッドになります.

wp-load.phpを呼び出して処理するのは脆弱性に繋がるので注意が必要です!

コードはご自由にご使用ください.Xのシェア、フォローして頂けると有り難いです☺️

App\Services\WordPressService.php
<?php

namespace App\Services;

use Illuminate\Support\Facades\DB;

class WordPressService
{
    public function randArticle(int $limit=2): array
    {
        $posts = DB::connection('wordpress')
            ->table('wp_posts as p')
            ->leftJoin('wp_postmeta as pm', function ($join) {
                $join->on('p.ID', '=', 'pm.post_id')
                    ->where('pm.meta_key', '_thumbnail_id');
            })
            ->leftJoin('wp_posts as img', 'pm.meta_value', '=', 'img.ID')
            ->select(
                'p.*',
                DB::raw('img.guid as thumbnail')
            )
            ->where('p.post_type', 'post')
            ->where('p.post_status', 'publish')
            ->where('p.post_date', '>=', now()->subMonths(3))
            ->inRandomOrder()
            ->limit($limit)
            ->get();
        $article = [];
        $i = 0;
        if ($posts->isNotEmpty()) { // 記事が見つかった場合
            foreach ($posts as $post) {
                $article[$i]['link']  = $post->guid;
                $article[$i]['title'] = $post->post_title;
                // Y-m-d 形式にフォーマットして代入
                $article[$i]['date']  = date('Y-m-d', strtotime($post->post_date));
                $article[$i]['imagePath'] = $post->thumbnail;
                $i++;
            }
        } else { // 記事が見つからなかった場合
            echo '<p>過去3ヶ月以内の記事が見つかりませんでした。</p>';
        }
        return $article;
    }

    public function newArticle(int $limit=3): array
    {
        $posts = DB::connection('wordpress')
            ->table('wp_posts as p')
            ->leftJoin('wp_postmeta as pm', function ($join) {
                $join->on('p.ID', '=', 'pm.post_id')
                    ->where('pm.meta_key', '_thumbnail_id');
            })
            ->leftJoin('wp_posts as img', 'pm.meta_value', '=', 'img.ID')
            ->select(
                'p.*',
                DB::raw('img.guid as thumbnail')
            )
            ->where('p.post_type', 'post')
            ->where('p.post_status', 'publish')
            ->where('p.post_date', '>=', now()->subMonths(3))
            ->orderBy('p.post_date', 'desc')
            ->limit($limit)
            ->get();
        $article = [];
        $i = 0;
        if ($posts->isNotEmpty()) { // 記事が見つかった場合
            foreach ($posts as $post) {
                $article[$i]['link']  = $post->guid;
                $article[$i]['title'] = $post->post_title;
                // Y-m-d 形式にフォーマットして代入
                $article[$i]['date']  = date('Y-m-d', strtotime($post->post_date));
                $article[$i]['imagePath'] = $post->thumbnail;
                $i++;
            }
        } else { // 記事が見つからなかった場合
            echo '<p>新着記事が見つかりませんでした。</p>';
        }

        return array_reverse($article);
    }
}
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?