我正在努力使用WordPress搜索功能。如果有人能向我解释一下内置的WordPress功能是如何工作的,也许会有所帮助。但我想要的是:
两个搜索字段,按名称和位置,第一个字段应检查所有常见帖子数据(仅帖子)+类别,第二个字段应仅检查帖子自定义字段,只需填写一个字段。首先,这是可能的还是必须使用本机php+sql?如果可能的话,你能告诉我从哪里开始吗?
我正在努力使用WordPress搜索功能。如果有人能向我解释一下内置的WordPress功能是如何工作的,也许会有所帮助。但我想要的是:
两个搜索字段,按名称和位置,第一个字段应检查所有常见帖子数据(仅帖子)+类别,第二个字段应仅检查帖子自定义字段,只需填写一个字段。首先,这是可能的还是必须使用本机php+sql?如果可能的话,你能告诉我从哪里开始吗?
内置的WP搜索看起来only a发布“/页”标题和内容,not 通过其他元素,如自定义字段和短代码的内容。有许多插件可以搜索自定义字段:
http://wordpress.org/extend/plugins/wp-custom-fields-search/
http://wordpress.org/extend/plugins/search-everything/
http://wordpress.org/extend/plugins/relevanssi/
还有一个过滤器apply_filters_ref_array()
这“允许插件在上下文中添加/删除/修改数据库查询的搜索部分。”仅供参考,搜索定义见query.php
, 看起来像:
...if ( !empty($q[\'s\']) )
...
($wpdb->posts.post_title LIKE \'{$n}{$term}{$n}\') OR ($wpdb->posts.post_content LIKE \'{$n}{$term}{$n}\')
您可以通过制作自己的搜索模板和使用WP_Query. 在大多数情况下,有足够的参数来执行您想要的操作,您不需要深入数据库。
例子:creating a custom search template.
如果您确实需要WP\\u Query所能做之外的东西,那么您不需要定制php/sql,您可以使用WordPress的WPDB类来interact with the database.
我有一个非常简单的解决方案:
打开你的门search.php
文件,将此代码粘贴到搜索文件的顶部。
function the_acf_search_func( $sql ){
global $wpdb;
$the_acf_search = get_query_var( \'the_acf_search\' );
global $s;
if( $the_acf_search ){
$rep = $wpdb->prepare( ") OR ( CAST(wp_postmeta.meta_value AS CHAR) LIKE \'%s\' ) OR (", \'%\'.$s.\'%\' );
$sql = str_replace(\') OR (\',$rep,$sql);
}
return $sql;
}
add_action( \'posts_where\', \'the_acf_search_func\' );
// var_dump($paged);die();
$args = array_merge( (array)$wp_query->query_vars, array(
\'posts_per_page\'=>999999,
// \'paged \'=>$p,
\'the_acf_search\'=>$s,
\'meta_query\' => array(
array(
\'value\' => \'\',
\'compare\' => \'!=\',
)
)
));
为了更好地理解这一点,还提前写了一些博客。。。http://www.jqui.net/wordpress/wp-search-custom-fields/希望有帮助。
如何使permalink可用于搜索结果页/?s=一+二+确定到/搜索/一-二确定/感谢