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?

WordPressのsitemap.xmlを作った時に利用した関数忘備録

Last updated at Posted at 2025-11-20

sitemap.xmlの作成

目的
WordPress用のsitemap.xmlを作りたい

フリーランスでWPカスタマイズを行っている友人は基本プラグインで済ませてるっすよ。と聞いていたのでそれでいいか。
と思って WordPress sitemap.xml を調べたが、とにかくプラグイン数が多い。
どれが正解と言う訳でもないだろうし、どれを入れれば見合った物になるか。と言う事で複数のプラグインをDocker環境に入れてみたが、どれも親切過ぎる。

本来の目的はsitemap.xmlが作りたいと言うだけなのだが

SEO対策どうっすか?
サイトの速度どうっすか?
なんならサイトのセキュリティまで面倒見ますよ!

と兎に角親切過ぎるので、ローカルの記事やカテゴリを確認したいだけだが色々と設定が面倒過ぎる。
結論から言えば自分で目的の部分だけエクスポート出来るようにした方が早くね?と。

前提条件

コマンドラインでかつPOSTデータを付与する形とする。

前提条件
if (php_sapi_name() !== 'cli') {
    exit("This script can only be run from the command line.");
}
if ($argv['0'] == "hogehogehogehoge" && $argv['1'] == 'fugafugafugafuga'){
    ##  処理する

WPなので狙われる事を前提にする。

取得に利用した関数

関数
## カテゴリ一覧
$categories = get_categories( array(
    'hide_empty' => true,
) );

## タグ一覧
$tags = get_tags( array(
    'hide_empty' => true,
) );

## 投稿者一覧
$authors = get_users( array(
    'who' => 'authors',
) );

## 投稿記事
$args = array(
    'post_type' => 'post',
    'posts_per_page' => -1, // 全ての投稿を取得
    'post_status' => 'publish', // 公開されている投稿のみ
);
$posts = new WP_Query( $args );

あとはforeachで出力するsitemap.xmlに書き込むと言う形で対応。
プラグインは便利だし親切だけど目的の本質にたどり着くまでに設定とかが多い。
時間があれば設定を全て読み込んでも良いけど、最短距離では書いた方が早い物もある。

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?