query_posts

函式
query_posts ( $query )
引數
  • (array|string) $query Array or string of WP_Query arguments.
    Required:
返回值
  • (WP_Post[]|int[]) Array of post objects or post IDs.
定義位置
相關方法
get_postscount_user_postsnext_postshave_poststhe_post
引入
1.5.0
棄用
-

query_posts: 這是WordPress中的一個函式,用於修改WordPress頁面或文章的主要查詢。它允許開發者指定自定義引數來檢索特定的文章或頁面。

設定帶有查詢引數的The Loop。

注意: 這個函式將完全覆蓋主查詢,不打算給外掛或主題使用。它對主查詢的修改過於簡單,可能會產生問題,應儘可能避免。在大多數情況下,有更好、更有效的方法來修改主查詢,例如通過WP_Query中的{@see ‘pre_get_posts’}動作。

這不能在WordPress Loop中使用。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function query_posts( $query ) {
$GLOBALS['wp_query'] = new WP_Query();
return $GLOBALS['wp_query']->query( $query );
}
function query_posts( $query ) { $GLOBALS['wp_query'] = new WP_Query(); return $GLOBALS['wp_query']->query( $query ); }
function query_posts( $query ) {
	$GLOBALS['wp_query'] = new WP_Query();
	return $GLOBALS['wp_query']->query( $query );
}

常見問題

FAQs
檢視更多 >