wp_count_posts()


WordPressのwp_count_posts()関数は、投稿タイプごとの投稿数を取得するために使用されます。この関数は、公開済みの投稿数や下書きの投稿数など、さまざまなステータスの投稿数を返します。

構文

wp_count_posts( string $type = 'post', string $perm = '' );

引数の説明:

  • $type (string) — 投稿タイプを指定します。デフォルトは’post’です。
  • $perm (string) — パーミッションを指定します。通常は空のままにします。

例1: 公開済みの投稿数を取得する

この例では、公開済みの投稿数を取得します。

<?php $count_posts = wp_count_posts(); echo $count_posts->publish; ?>

例2: カスタム投稿タイプの投稿数を取得する

カスタム投稿タイプの投稿数を取得する例です。

<?php $count_posts = wp_count_posts('custom_post_type'); echo $count_posts->publish; ?>

例3: 下書きの投稿数を取得する

下書きの投稿数を取得する例です。

<?php $count_posts = wp_count_posts(); echo $count_posts->draft; ?>

例4: 予約済みの投稿数を取得する

予約済みの投稿数を取得する例です。

<?php $count_posts = wp_count_posts(); echo $count_posts->future; ?>

例5: ゴミ箱内の投稿数を取得する

ゴミ箱内の投稿数を取得する例です。

<?php $count_posts = wp_count_posts(); echo $count_posts->trash; ?>

例6: 非公開の投稿数を取得する

非公開の投稿数を取得する例です。

<?php $count_posts = wp_count_posts(); echo $count_posts->private; ?>

例7: 特定のユーザーの投稿数を取得する

特定のユーザーの投稿数を取得する例です。

<?php $count_posts = wp_count_posts('post', 'readable'); echo $count_posts->publish; ?>

例8: すべての投稿ステータスの投稿数を取得する

すべての投稿ステータスの投稿数を取得する例です。

<?php $count_posts = wp_count_posts(); print_r($count_posts); ?>

注意点

  • この関数はデータベースにクエリを実行するため、頻繁に使用するとパフォーマンスに影響を与える可能性があります。
  • カスタム投稿タイプを使用する場合は、正しい投稿タイプ名を指定してください。

関連機能: