我可以覆盖或扩展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%')) ...
的{{1}} ...,就像$wp_query
一样?
我一直在寻找使用搜索查询的当前函数,但我还没有找到它,所以这就是为什么我在寻找扩展或覆盖它的原因。
答案 0 :(得分: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'