WordPress の WP_Query オブジェクトの中身です。メイン クエリの $wp_query オブジェクトを var_dump して一覧表にしました。検索しても見つからなかったので作成しました。

WP_Query とは
この説明は難しい。でも、WP_Query オブジェクトの中身を知りたくて検索した方々なら、おそらく何となく理解しているはず。それです。詳しくは以下の Codex ページをご参照ください。
- 関数リファレンス/WP Query – WordPress Codex 日本語版
- Class Reference/WP Query « WordPress Codex
- WordPress > WP_Query | Class | WordPress Developer Resources
WP_Query オブジェクト($wp_query)のプロパティ一覧
| Property | Sub Property | Data Type | Note |
|---|---|---|---|
| query | array | ||
| query_vars | array | クエリ条件の連想配列 | |
| attachment | string | ||
| attachment_id | int | ||
| author | string | ||
| author__in | array | ||
| author__not_in | array | ||
| author_name | string | ||
| cache_results | bool | ||
| cat | string | ||
| category__and | array | ||
| category__in | array | ||
| category__not_in | array | ||
| category_name | string | ||
| comments_per_page | string | ||
| comments_popup | string | ||
| day | int | ||
| error | string | ||
| feed | string | ||
| fields | string | ||
| hour | string | ||
| ignore_sticky_posts | bool | ||
| m | string | ||
| menu_order | string | ||
| meta_key | string | ||
| meta_value | string | ||
| minute | string | ||
| monthnum | int | ||
| name | string | ||
| no_found_rows | bool | クエリー結果をページ分割しないなら True にするとパフォーマンスが向上する,get_posts() 関数のデフォルトは True | |
| nopaging | bool | ||
| p | int | ||
| page_id | int | ||
| paged | int | 表示中ページが何ページ目か(1ページ目は 0,2ページ目以降は 2~) | |
| pagename | string | ||
| post__in | array | ||
| post__not_in | array | ||
| post_parent | string | ||
| post_parent__in | array | ||
| post_parent__not_in | array | ||
| post_type | string | ||
| posts_per_page | int | 1ページに何件の投稿を表示するか | |
| preview | string | ||
| s | string | ||
| second | string | ||
| sentence | string | ||
| static | string | ||
| subpost | string | ||
| subpost_id | string | ||
| suppress_filters | bool | True にするとクエリ関連のフィルター フックが無効になる,get_posts() 関数のデフォルトは True なので注意 | |
| tag | string | ||
| tag__and | array | ||
| tag__in | array | ||
| tag__not_in | array | ||
| tag_id | string | ||
| tag_slug__and | array | ||
| tag_slug__in | array | ||
| tb | string | ||
| update_post_meta_cache | bool | ||
| update_post_term_cache | bool | ||
| w | int | ||
| year | int | ||
| order | string | ||
| tax_query | object | WP_Tax_Query オブジェクト | |
| queries | array | ||
| relation | string | ||
| meta_query | object | WP_Meta_Query オブジェクト | |
| queries | array | ||
| relation | NULL | ||
| date_query | bool | ||
| comment_count | int | ||
| current_comment | int | ||
| current_post | int | ※ループ内のみ有効 表示中の投稿がループ内の何番目か(0 からカウント) |
|
| found_posts | string | クエリ条件に一致する投稿の合計数(なぜか string 型) | |
| max_num_comment_pages | int | ||
| max_num_pages | float | 全ページ数 | |
| post | object | ※ループ内のみ有効 表示中の WP_Post オブジェクト |
|
| post_count | int | 表示中ページの全 WP_Post オブジェクトの数 | |
| posts | &array | 表示中ページの全 WP_Post オブジェクトの配列 | |
| request | string | 表示中ページのクエリの SELECT 文 | |
| thumbnails_cached | bool | ||
| in_the_loop | bool | ループ内の場合 True | |
| is_404 | bool | ||
| is_admin | bool | ||
| is_archive | bool | ||
| is_attachment | bool | ||
| is_author | bool | ||
| is_category | bool | ||
| is_comment_feed | bool | ||
| is_comments_popup | bool | ||
| is_date | bool | ||
| is_day | bool | ||
| is_feed | bool | ||
| is_home | bool | ||
| is_month | bool | ||
| is_page | bool | ||
| is_paged | bool | ||
| is_post_type_archive | bool | ||
| is_posts_page | bool | ||
| is_preview | bool | ||
| is_robots | bool | ||
| is_search | bool | ||
| is_single | bool | ||
| is_singular | bool | ||
| is_tag | bool | ||
| is_tax | bool | ||
| is_time | bool | ||
| is_trackback | bool | ||
| is_year | bool | ||
| queried_object | NULL | ||
| queried_object_id | int | ||
| query_vars_hash:WP_Query:private | string | ||
| query_vars_changed:WP_Query:private | bool | ||
| stopwords:WP_Query:private | NULL | ||
備考
Data Type が NULL になっているプロパティは、var_dump したとき そうだったままです。堪忍してください。
query_vars について
以下のページに詳細があります。これは便利! 多謝です。
- WP_Queryの使い方をPHPコードにまとめた便利なコード・スニペット | notnil creation weblog
- WP: Query $args(上記コード スニペットの英語原文版)
global $paged について
ページネーションを自前で実装する際などに使われている $paged グローバル変数。実は このグローバル変数へのアクセスは推奨されていません(と Codex に書いてあります)。$paged は WP_Query オブジェクトの query_vars[‘paged’] なので、get_query_var() 関数で取得してローカル変数として使うのが安全なお作法です。
$paged = get_query_var( 'paged', 1 );
同様に、global $posts_per_page も非推奨なので、以下で取得しましょう。
$posts_per_page = get_query_var( 'posts_per_page' );
- Function Reference/get query var « WordPress Codex
- 関数リファレンス/get query var – WordPress Codex 日本語版
- Global Variables « WordPress Codex
is_xxxxx プロパティについて
is_home,is_category などの is_xxxxx プロパティについては、対応する条件分岐タグ(Conditional Tags)や後述するメソッドを使用するのが安全です。
WP_Query オブジェクト($wp_query)のメソッド一覧
さきほどリンクした以下の Developer Resources ドキュメントに一覧されています。
関連情報
あわせて読みたい。WP_Query を使うときの Tips です。
- query_postsを捨てよ、pre_get_postsを使おう | notnil creation weblog
- 10upのWordPressのベストプラクティスから「効率的なデータベースクエリー」の章を訳してみた | わーどぷれすっ!
最後のベスト プラクティスに、「get_posts() は使わない方が良い」旨が書いてあります。get_posts() 関数と WP_Query::get_posts() メソッドの どちらのことだろうと思ってソース コードを当たってみたところ、get_posts() 関数は WP_Query::get_posts() メソッドを呼んでいるので、どちらにせよ使わない方が良い、と理解。
これで WP_Query オブジェクトのすべては あなたのものだ。