get_all_page_ids()
云策文档标注
概述
get_all_page_ids() 函数用于获取所有页面(post_type 为 'page')的 ID 列表。它通过数据库查询并利用缓存机制提高性能。
关键要点
- 返回一个字符串数组,包含所有页面的 ID。
- 使用 wp_cache_get() 和 wp_cache_add() 进行缓存优化,避免重复查询数据库。
- 内部通过 $wpdb->get_col() 执行 SQL 查询,从 $wpdb->posts 表中筛选 post_type = 'page' 的记录。
代码示例
$page_ids = get_all_page_ids();
echo 'My Page List :';
foreach($page_ids as $page) {
echo '<br>' . get_the_title($page);
}
原文内容
Gets a list of page IDs.
Source
function get_all_page_ids() {
global $wpdb;
$page_ids = wp_cache_get( 'all_page_ids', 'posts' );
if ( ! is_array( $page_ids ) ) {
$page_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type = 'page'" );
wp_cache_add( 'all_page_ids', $page_ids, 'posts' );
}
return $page_ids;
}
Changelog
| Version | Description |
|---|---|
| 2.0.0 | Introduced. |
Skip to note 2 content
Codex
Basic Example
My Page List :</h3>'; foreach($page_ids as $page) { echo '<br />'.get_the_title($page); } ?>