2014-02-27 60 views
1

有可能我可以覆蓋或擴展wordpress的搜索查詢嗎?更新WordPress的搜索查詢

當前的搜索是這樣的:

http://my-wp-site/?s=test&cp_city=Alaminos&sa=search&scat=0 
在我的主題search.php中

我試圖打印$wp_query並返回:

...... 
     WP_Query Object ([query] => Array ([s] => test [scat] => 0) [query_vars] => Array ([s] => test [scat] => 0 [error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author] => [author_name] => [feed] => [tb] => [paged] => 0 [comments_popup] => [meta_key] => [meta_value] => [preview] => [sentence] => [fields] => [menu_order] => [category__in] => Array () [category__not_in] => Array () [category__and] => Array () [post__in] => Array () [post__not_in] => Array () [tag__in] => Array () [tag__not_in] => Array () [tag__and] => Array () [tag_slug__in] => Array () [tag_slug__and] => Array () [post_parent__in] => Array () [post_parent__not_in] => Array () [author__in] => Array () [author__not_in] => Array () [ignore_sticky_posts] => [suppress_filters] => [cache_results] => 1 [update_post_term_cache] => 1 [update_post_meta_cache] => 1 [post_type] => any [posts_per_page] => 10 [nopaging] => [comments_per_page] => 50 [no_found_rows] => [search_terms_count] => 1 [search_terms] => Array ([0] => test) [search_orderby_title] => Array ([0] => zfy_posts.post_title LIKE '%test%') [order] => DESC) [tax_query] => WP_Tax_Query Object ([queries] => Array () [relation] => AND) [meta_query] => WP_Meta_Query Object ([queries] => Array () [relation] =>) [date_query] => [queried_object] => [queried_object_id] => 0 [request] => SELECT SQL_CALC_FOUND_ROWS zfy_posts.ID FROM zfy_posts INNER JOIN zfy_term_relationships AS r ON (zfy_posts.ID = r.object_id) INNER JOIN zfy_term_taxonomy AS x ON (r.term_taxonomy_id = x.term_taxonomy_id) AND (x.taxonomy = 'ad_tag' OR x.taxonomy = 'ad_cat' OR 1=1) INNER JOIN zfy_postmeta AS m ON (zfy_posts.ID = m.post_id) INNER JOIN zfy_terms AS t ON x.term_id = t.term_id WHERE 1=1 AND (((zfy_posts.post_title LIKE '%test%') OR (zfy_posts.post_content LIKE '%test%') OR ((t.name LIKE '%test%')) OR ((t.slug LIKE '%test%')) OR ((m.meta_key = 'cp_price') AND (m.meta_value LIKE '%test%')) OR ((m.meta_key = 'cp_street') AND (m.meta_value LIKE '%test%')) OR ((m.meta_key = 'cp_city') AND (m.meta_value LIKE '%test%')) OR ((m.meta_key = 'cp_state') AND (m.meta_value LIKE '%test%')) OR ((m.meta_key = 'cp_country') AND (m.meta_value LIKE '%test%')) OR ((m.meta_key = 'cp_zipcode') AND (m.meta_value LIKE '%test%')) OR ((m.meta_key = 'cp_region') AND (m.meta_value LIKE '%test%')) OR ((m.meta_key = 'cp_size') AND (m.meta_value LIKE '%test%')) OR ((m.meta_key = 'cp_feedback') AND (m.meta_value LIKE '%test%')) OR ((m.meta_key = 'cp_currency') AND (m.meta_value LIKE '%test%')))) AND (zfy_posts.post_status = 'publish') AND (zfy_posts.post_type IN ('ad_listing','post','page')) GROUP BY zfy_posts.ID ORDER BY zfy_posts.post_title LIKE '%test%' DESC, zfy_posts.post_date DESC LIMIT 0, 10 [posts] => Array ([0] => WP_Post Object ([ID] => 92 [post_author] => 4 [post_date] => 2014-02-22 12:33:40 [post_date_gmt] => 2014-02-22 12:33:40 [post_content] => 
     fg fdgfdg 
    ....... 

是有可能,我可以將其覆蓋在functions.php或在search.php?我只想添加值爲cp_city的GET值並將其替換爲...OR ((m.meta_key = 'cp_city') AND (m.meta_value LIKE '%test%')) ...從當前的$wp_query ...將會像...OR ((m.meta_key = 'cp_city') AND (m.meta_value LIKE '$_GET["cp_city"]')) ...那樣?

我一直在尋找使用搜索查詢的當前函數,但我還沒有找到它,所以這就是我尋找擴展或覆蓋它的原因。

回答

1

您可以覆蓋模板中的$wp_query變量。

從文檔修改:

$args = array(
    'post_type' => 'my_custom_post_type', 
    'meta_key' => 'cp_city', 
    'orderby' => 'meta_value_num', 
    'order' => 'ASC', 
    'meta_query' => array(
     array(
      'key' => 'cp_city', 
      'value' => $_GET['cp_city'], 
     ) 
    ) 
); 
$wp_query = new WP_Query($args); 

http://codex.wordpress.org/Class_Reference/WP_Query搜索頁面'meta_query'