[WordPress General] ページテンプレート内の条件分岐に応じて 404 を表示させる

例えば if 文の分岐によって読み込むテンプレートを 404 にしたい場合。

> * [404 error – Redirect Restricted Page to 404 – WordPress Development Stack Exchange](https://wordpress.stackexchange.com/questions/24891/redirect-restricted-page-to-404/27124#27124)

“`php
if ( 条件 ) {
// 何らかの処理
} else {
// 404 を表示させる
global $wp_query;
$wp_query->set_404();
status_header( 404 );
get_template_part( 404 );
exit();
}
“`

メモ的に。

> * [status_header() | Function | WordPress Developer Resources](https://developer.wordpress.org/reference/functions/status_header/)

## 補遺

> * [404エラーページの作成 – WordPress Codex 日本語版](https://wpdocs.osdn.jp/404%E3%82%A8%E3%83%A9%E3%83%BC%E3%83%9A%E3%83%BC%E3%82%B8%E3%81%AE%E4%BD%9C%E6%88%90)
> * [WordPressで404ページを作成 \| EasyRamble](https://easyramble.com/wordpress-404-not-found-page.html)