我有这个奇怪的问题.我想获取所有自定义类型的帖子,这是我的代码段.
$query = new WP_Query(array(
'post_type' => 'custom',
'post_status' => 'publish'
));
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
echo $post_id;
echo "<br>";
}
wp_reset_query();
Run Code Online (Sandbox Code Playgroud)
这只能获得其中的6个,而我在数据库中有超过50条符合该条件的记录.谁能告诉我哪里出错了?
非常感谢!
And*_*rea 26
这将获得自定义类型的所有帖子get_posts:
$posts = get_posts([
'post_type' => 'custom',
'post_status' => 'publish',
'numberposts' => -1
// 'order' => 'ASC'
]);
Run Code Online (Sandbox Code Playgroud)
要返回的帖子数量在设置>阅读下设置
您可以传递要使用的查询的帖子数.
'posts_per_page' => 'number of posts'
Run Code Online (Sandbox Code Playgroud)
小智 6
您永远不要使用:
'posts_per_page' => -1
Run Code Online (Sandbox Code Playgroud)
如果您谈论的是SQL查询速度,它的运行速度会很慢且无效。因此,最好使用一些大整数。
这是性能危险。如果我们有100,000个帖子怎么办?这可能会使站点崩溃。例如,如果您正在编写小部件,并且只想获取所有自定义帖子类型,请为您的情况确定一个合理的上限。
此处有更多详细信息:https : //10up.github.io/Engineering-Best-Practices/php/#performance