「特定の投稿タイプで“いちばん新しい投稿”の ID だけ知りたい」――一覧の先頭リンクや“最新号”の詳細ページへの導線づくりで、こうした場面はよくあります。ここでは WP_Query を最小限で回して最新の投稿IDだけを返す、軽量なユーティリティ関数を紹介します。
(※解説ではベンダー名・接頭辞を一切使わない汎用表記にしています)
できること(概要)
- 投稿タイプを指定して 最新1件の投稿ID を返す
- タクソノミー/メタクエリ/著者/親固定ページ などで絞り込み
- 取得順序のカスタマイズ(
orderby
,order
) - トランジェント(分単位)での任意キャッシュ
- 何も見つからないときは
0
を返す(扱いやすい)
実装コード(汎用名)
<?php
if (!function_exists('get_latest_post_id')) {
function get_latest_post_id(string $post_type, array $args = []): int
{
$opt = array_merge([
'post_status' => 'publish',
'author' => null,
'post_parent' => null,
'tax_query' => null,
'meta_query' => null,
'orderby' => 'date',
'order' => 'DESC',
'cache_min' => 0,
'suppress_filters' => true,
], $args);
$cache_key = 'latest_id_' . md5(wp_json_encode([
'post_type' => $post_type,
'post_status' => $opt['post_status'],
'author' => $opt['author'],
'post_parent' => $opt['post_parent'],
'tax_query' => $opt['tax_query'],
'meta_query' => $opt['meta_query'],
'orderby' => $opt['orderby'],
'order' => $opt['order'],
]));
if ((int)$opt['cache_min'] > 0) {
$cached = get_transient($cache_key);
if ($cached !== false) {
return (int) $cached;
}
}
$query_args = [
'post_type' => $post_type,
'post_status' => $opt['post_status'],
'posts_per_page' => 1,
'orderby' => $opt['orderby'],
'order' => $opt['order'],
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'suppress_filters' => $opt['suppress_filters'],
];
if (!is_null($opt['author'])) $query_args['author'] = $opt['author'];
if (!is_null($opt['post_parent'])) $query_args['post_parent'] = (int) $opt['post_parent'];
if (!empty($opt['tax_query'])) $query_args['tax_query'] = $opt['tax_query'];
if (!empty($opt['meta_query'])) $query_args['meta_query'] = $opt['meta_query'];
$q = new WP_Query($query_args);
$post_id = 0;
if ($q->have_posts()) {
$q->the_post();
$post_id = (int) get_the_ID();
wp_reset_postdata();
}
if ((int)$opt['cache_min'] > 0) {
set_transient($cache_key, $post_id, MINUTE_IN_SECONDS * (int)$opt['cache_min']);
}
return $post_id;
}
}
使い方(スニペット集)
もっとも基本的な使い方
$latest_id = get_latest_post_id('your_cpt');
if ($latest_id) {
echo get_permalink($latest_id);
}
タクソノミーでカテゴリを絞る
$latest_id = get_latest_post_id('your_cpt', [
'tax_query' => [[
'taxonomy' => 'your_cpt_category',
'field' => 'term_id',
'terms' => [12],
]],
]);
メタキーで「公開フラグ=1」を絞る + 10分キャッシュ
$latest_id = get_latest_post_id('your_cpt', [
'meta_query' => [[
'key' => 'publish_flag',
'value' => '1',
]],
'cache_min' => 10,
]);
年度(カスタムフィールド)で並び替える
$latest_id = get_latest_post_id('your_cpt', [
'orderby' => ['meta_value_num' => 'DESC', 'date' => 'DESC'],
'meta_query' => [[
'key' => 'fiscal_year',
'type'=> 'NUMERIC',
]],
]);
コメント