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?

More than 1 year has passed since last update.

【WordPress】get_posts の総ページ数を取得したい場合

Posted at

ページネーション実装時などで総ページ数を取得したい場合、 get_posts() では取得できない問題がある。

$args = [...];
$post_list = get_post($args);

foreach($post_list as $post) {
    ...
}
// page_max は?取れる方法がありません。

以下で同じ $post_list を取得しつつ総ページ数も取得できるっぽい。

$args = [...];
$q = new WP_Query($args);
$post_list = $q->get_posts();

foreach($post_list as $post) {
    ...
}

$q->max_num_pages;

WordPress によく慣れている人なら have_posts()the_post() 使って書くのが普通なのかもしれないけど
あれ何やってるか分からん…という人向けの解決方法でした

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?