- 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細)
関数リファレンス/get query var
このページ「関数リファレンス/get query var」は一部未翻訳です。和訳や日本語情報を加筆してくださる協力者を求めています。
目次
説明
Retrieve public query variable in the WP_Query class of the global $wp_query object.
使い方
<?php get_query_var( $var ) ?>
パラメーター
- $var
- (string) (必須) The variable key to retrieve.
- 初期値: なし
戻り値
- (mixed)
returns empty string if var is not set
例
現在のページネーション番号を取得する
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?> <h1>Currently Browsing Page <?php echo $paged; ?></h1>
For getting the current pagination number on a static front page (Page template) you have to use the 'page' query variable:
<?php $paged = (get_query_var('page')) ? get_query_var('page') : 1; ?> <h1>Currently Browsing Page <?php echo $paged; ?> On a static front page</h1>
Note: The query variable 'page' holds the pagenumber for a single paginated Post or Page that includes the <!--nextpage-->
Quicktag in the post content.
注
get_query_var() only retrieves public query variables that are recognized by WP_Query. This means that if you create your own custom URLs with their own query variables, get_query_var() will not retrieve them without some further work (see below).
カスタムクエリ変数
In order to be able to add and work with your own custom query vars that you append to URLs (eg: "http://mysite.com/some_page/?my_var=foo" - for example using add_query_arg()) you need to add them to the public query variables available to WP_Query. These are built up when WP_Query instantiates, but fortunately are passed through a filter 'query_vars' before they are actually used to populate the $query_vars property of WP_Query.
So, to expose your new, custom query variable to WP_Query hook into the 'query_vars' filter, add your query variable to the $vars array that is passed by the filter, and remember to return the array as the output of your filter function. See below:
function add_query_vars_filter( $vars ){ $vars[] = "my_var"; return $vars; } add_filter( 'query_vars', 'add_query_vars_filter' );
- See WP_Query::get()
- Uses global: (object) $wp_query
変更履歴
1.5.0 から導入されました。
ソースファイル
get_query_var() は wp-includes/query.php
にあります。
関連
記事
- 記事: ループ - WordPress ループ内でのクエリの使い方に関する基本的な概要。
- 記事: クエリ概要 - どのクエリが WordPress を生成するのかが決定される方法についての説明。
- 記事: フックを使ったクエリのカスタマイズ
- 記事: カスタムセレクトクエリを使った投稿の表示 /en
- 記事: 高度なタクソノミークエリの生成 /en
- 記事: オフセットとページネーションを活用したカスタムクエリ /en
コード・ドキュメンテーション
- クラス: WP_Query - WP_Query クラスの詳細な全容
- クラス: WP_Comment_Query - コメント関連のクエリのためのクラス
- クラス: WP_User_Query - ユーザー関連のクエリのためのクラス
- オブジェクト: $wpdb - $wpdb オブジェクトの使い方全容
- 関数: set_query_var()
- 関数: get_query_var()
- 関数: query_posts() - 追加のカスタムクエリを作成
- 関数: get_post() - 項目の ID を取得しデータベース内にあるその投稿のレコードを返す
- 関数: get_posts() - 投稿の配列を返すことに特化した関数
- 関数: get_pages() - ページの配列を返すことに特化した関数
- 関数: have posts() - クエリが投稿を返すか否かを判断する条件関数
- 関数: the_post() - クエリ後に自動的にループを設定する
- 関数: rewind_posts() - 現状のループをリセットする
- 関数: setup_postdata() - ループ内で個別の結果を得るためのクエリデータを設定する
- 関数: wp_reset_postdata() - 直前のクエリを復元する (通常はループ内の別のループの後に用いられる)
- 関数: wp_reset_query()
- 関数: is_main_query() - 変更されるクエリがメインのクエリであることを確認する
- アクションフック: pre_get_posts - WordPressクエリが実行される前に変更する
- アクションフック: the_post - post クエリの後で post オブジェクトを変更する
- フィルターフック: found_posts - WP_Query オブジェクトの found_posts 値を変更する
最新英語版: WordPress Codex » Function Reference/get query var (最新版との差分)