自定义搜索自定义帖子类型,自定义元和搜索字段
-
-
[您是否看到了这个答案](http://wordpress.stackexchange.com/questions/66815/extending-search-query-with-additional-sentence-value/66904#66904)?[Have you seen this answer](http://wordpress.stackexchange.com/questions/66815/extending-search-query-with-additional-sentence-value/66904#66904)?
- 0
- 2013-07-08
- kaiser
-
我不是,但是对于我想做的事情来说似乎非常复杂?I'd not but it seems massively complex for what I'm trying to do?
- 0
- 2013-07-08
- James J
-
"非常复杂"意味着您不想阅读那么多文字,对吗?:P请阅读.它将使您跌到最低的一半."massively complex" means you don't want read that much text, right? :P Please read it. It will bring you down half the way minimum.
- 1
- 2013-07-08
- kaiser
-
在这个问题上有什么进展吗?Any progress on that question?
- 0
- 2013-08-01
- kaiser
-
2 个回答
- 投票数
-
- 2013-07-08
如果要扩展查询,则应通过
pre_get_posts
-filter扩展它.然后只需执行"自定义字段"或元查询.add_action( 'pre_get_posts', 'wpse105969_extended_search' ); function wpse105969_extended_search( $query ) { // Make sure we got a search query // and we're only modifying the main query. if ( ! $query->is_main_query() AND '' === $query->get( 's' ) AND 'your_custom_post_type' === $query->get( 'post_type' ) ) return $query; // Alter whatever you need: Make, Model, etc. $query->set( 'meta_query', array( 'relation' => 'OR', array( 'key' => 'color', 'value' => 'blue', 'compare' => 'NOT LIKE' ), array( 'key' => 'price', 'value' => array( 20, 100 ), 'type' => 'numeric', 'compare' => 'BETWEEN' ) ) ); return $query; }
If you want to extend your query, you should extend it through the
pre_get_posts
-filter. Then just do a "Custom Field" or meta query.add_action( 'pre_get_posts', 'wpse105969_extended_search' ); function wpse105969_extended_search( $query ) { // Make sure we got a search query // and we're only modifying the main query. if ( ! $query->is_main_query() AND '' === $query->get( 's' ) AND 'your_custom_post_type' === $query->get( 'post_type' ) ) return $query; // Alter whatever you need: Make, Model, etc. $query->set( 'meta_query', array( 'relation' => 'OR', array( 'key' => 'color', 'value' => 'blue', 'compare' => 'NOT LIKE' ), array( 'key' => 'price', 'value' => array( 20, 100 ), 'type' => 'numeric', 'compare' => 'BETWEEN' ) ) ); return $query; }
-
嗨,谢谢您的回答-我实际上没有使用普通wordpress搜索所需的's'字段(仅搜索自定义帖子类型的元详细信息).无论如何,有没有在搜索表单中不使用's'字段,以寻找有关此信息的方法.再次感谢.Hi thanks for the answer - I dont actually use the 's' field that normal wordpress searches require (im only search for the custom post types meta details). Is there anyway to NOT use the 's' field in the search form, struggling to find info on this. Thanks again.
- 0
- 2013-07-08
- James J
-
另外,我已经将您的代码添加到我的functions.php中,并添加到了我的字段中,但是当我保存它时,它会破坏wordpress(没有错误).Wordpress作品(导航,帖子等)一无所有.Also, I've added your code to my functions.php and added in my fields but when i save it breaks wordpress (no errors). Nothing from wordpress works (nav, posts etc).
- 0
- 2013-07-08
- James J
-
@JamesJ请使用_your_代码发布更新.提示:您是否更改了自定义帖子类型名称,并且将WP_DEBUG设置为true?@JamesJ Please post an update with _your_ code. Hint: Did you change the custom post type name and did you set `WP_DEBUG` to `true`?
- 0
- 2013-07-09
- kaiser
-
- 2018-08-07
这是代码.您可以根据需要更改
$post_type
和$custom_fields
.function extend_admin_search( $query ) { // Extend search for document post type $post_type = 'document'; // Custom fields to search for $custom_fields = array( "_file_name", ); if( ! is_admin() ) return; if ( $query->query['post_type'] != $post_type ) return; $search_term = $query->query_vars['s']; // Set to empty, otherwise it won't find anything $query->query_vars['s'] = ''; if ( $search_term != '' ) { $meta_query = array( 'relation' => 'OR' ); foreach( $custom_fields as $custom_field ) { array_push( $meta_query, array( 'key' => $custom_field, 'value' => $search_term, 'compare' => 'LIKE' )); } $query->set( 'meta_query', $meta_query ); }; } add_action( 'pre_get_posts', 'extend_admin_search' );
Here’s the code. You can change
$post_type
and$custom_fields
according to your needs.function extend_admin_search( $query ) { // Extend search for document post type $post_type = 'document'; // Custom fields to search for $custom_fields = array( "_file_name", ); if( ! is_admin() ) return; if ( $query->query['post_type'] != $post_type ) return; $search_term = $query->query_vars['s']; // Set to empty, otherwise it won't find anything $query->query_vars['s'] = ''; if ( $search_term != '' ) { $meta_query = array( 'relation' => 'OR' ); foreach( $custom_fields as $custom_field ) { array_push( $meta_query, array( 'key' => $custom_field, 'value' => $search_term, 'compare' => 'LIKE' )); } $query->set( 'meta_query', $meta_query ); }; } add_action( 'pre_get_posts', 'extend_admin_search' );
Id喜欢为特定的自定义帖子类型(车辆)构建搜索表单,并具有针对该自定义帖子类型的自定义元字段(价格,年龄)和自定义分类法(制造商)的过滤器.这将完全取代网站搜索,并且是唯一可用的搜索,因此我将在自定义模板中使用search.php.
我希望搜索看起来像这样:
搜索我们的车辆
制作(选中所有自定义分类法(奥迪,宝马等)的框).
模型(人们可以在其中输入任何内容的常规输入字段).
价格超过(选中价格从1000开始的框)
年龄(选择带有1年以下,3年以下,5年以下,10年以下的选项的框).
我是自定义字段的新手,并且真的不知道从哪里开始(我在Google上找到了一些示例,但没有一个完全符合我的目标).我也真的不想使用插件.我猜在search.php中,我抓取了从表单传递的数据,并用它来构建$ args并传递给WP_Query?
有人可以指出我正确的方向吗?预先感谢