2014-09-04 46 views
2

docs $查詢和$ query_vars之間的區別:什麼是在全球WP_Query

$query 
Holds the query string that was passed to the $wp_query object by WP class. 

$query_vars 
An associative array containing the dissected $query: an array of the query variables and their respective values. 

但是,當我做這樣的事情:

$args = array(
    'post_type' => 'post', 
    'posts_per_page' => 10, 
    'post_status' => 'publish' 
); 
$author_query = new WP_Query($args); 

那麼所有這些論點去$query_vars我可以從print_r($wp_query)中看到,那麼$query的目的是什麼,以及如何調整此屬性的值。

我很好奇,因爲當我去作者模板query屬性在這個頁面中包含像[author_name] => admin

回答

-1

$query_vars是一個數組,其中包含調用WP_Query時所傳遞的值,這是您的自定義值。

$query是一個數組,其中包含WP_Query支持的所有參數,包括調用WP_Query時的參數usen和其餘參數與默認值。

編輯:您看到author_name集的原因是Wordpress從URL獲取值,但您可以使用自定義查詢或某個過濾器來傳遞它。當Wordpress檢測到URL中的參數時,它會自動匹配正確的模板,您可以看到它在template hierarchy

+0

中的工作原理。它實際上是相反的。 '$ query'包含原始_raw_參數,'$ query_vars'包含_parsed_參數。 前者僅包含您傳遞的參數以及由URI提取的參數,後者包含所有未定義的變量填充爲空的變量。 – Chauncey 2018-01-25 15:10:23