英文:
Include tags and categories in AJAX search results in WordPress
问题
我们网站上有一个使用AJAX的搜索字段。它的目的是让访问者搜索特定的文章,它运作得很好,但搜索似乎只在文章的标题和内容中进行,我们希望它还可以搜索类别和标签。
下面您可以看到我们在functions.php中的代码。它只搜索文章的标题和内容,而不搜索它们的类别或标签。
==================
Ajax Search
======================
*/
// 添加AJAX获取JS
add_action('wp_footer', 'ajax_fetch');
function ajax_fetch() {
?>
<script type="text/javascript">
function fetch(){
jQuery.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'post',
data: { action: 'data_fetch', keyword: jQuery('#keyword').val() },
success: function(data) {
jQuery('#datafetch').html(data);
}
});
}
</script>
<?php
}
// AJAX功能
add_action('wp_ajax_data_fetch', 'data_fetch');
add_action('wp_ajax_nopriv_data_fetch', 'data_fetch');
function data_fetch(){
$the_query = new WP_Query( array( 'posts_per_page' => 8, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => array('post') ) );
if( $the_query->have_posts() ) :
...
我们的一些文章带有标签'male'或'female',标题中也可能包含单词'male'或'female'。当访客搜索单词'male'时,应该显示包含标签和/或标题中包含单词'male'的文章。但正如我所说,它只搜索标题和文章的内容,而不搜索它们的类别或标签,所以...
我们认为我们的问题可能通过在代码的这个区域中添加/更改一些内容来解决,但我们不确定具体是什么或在哪里。
<script type="text/javascript">
function fetch(){
jQuery.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'post',
data: { action: 'data_fetch', keyword: jQuery('#keyword').val() },
success: function(data) {
jQuery('#datafetch').html(data);
}
});
}
</script>
我们已经在互联网上搜索了解决方案,但我们找到的答案要么与问题无关,要么解决方案依赖于插件,我们希望通过修改代码来解决这个问题,而不是安装更多插件。
我们非常感谢您能提供的任何帮助。
已找到解决方案:
最初,我们不想安装任何额外的插件来解决这个问题,但由于没有找到令人满意的解决方案,我们尝试了WP Extended Search插件。它有效!
英文:
We have a search field on our website that uses AJAX. It's purpose is to let visitors search for specific posts, it works great but, the search seems to only search through the title and contents of the posts, we would like it to also search through categories and tags.
Below you can see our code in functions.php. It only searches through titles ans and contents of posts but not their categories or tags
==================
Ajax Search
======================
*/
// add the ajax fetch js
add_action( 'wp_footer', 'ajax_fetch' );
function ajax_fetch() {
?>
<script type="text/javascript">
function fetch(){
jQuery.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'post',
data: { action: 'data_fetch', keyword: jQuery('#keyword').val() },
success: function(data) {
jQuery('#datafetch').html( data );
}
});
}
</script>
<?php
}
// the ajax function
add_action('wp_ajax_data_fetch' , 'data_fetch');
add_action('wp_ajax_nopriv_data_fetch','data_fetch');
function data_fetch(){
$the_query = new WP_Query( array( 'posts_per_page' => 8, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => array('post') ) );
if( $the_query->have_posts() ) :
...
Some of our posts have the tag 'male' or 'female' and the title might also contain the words male of female. When a visitor searches the word male it should show the posts with the word male in the tag and/or the title ect. But as I said it only searches through titles and and contents of posts but not their categories or tags, so...
We think our problem might be solved by adding/changing something in this area of the code, but we are not sure what or where exactly.
<script type="text/javascript">
function fetch(){
jQuery.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'post',
data: { action: 'data_fetch', keyword: jQuery('#keyword').val() },
success: function(data) {
jQuery('#datafetch').html( data );
}
});
}
</script>
We have searched the internet for the answer to our problem however, the answers we find are either not related or the solution relies on a plugin, and we would like to fix this thought code and not installing more plugins.
We appreciate any help we can get.
A SOLUTION WAS FOUND:
We originally did not want to install any additional plugins to solve this problem however, because we did not find a satisfactory solution we resorted to trying the WP Extended Search plugin. And it works!
答案1
得分: 0
找到了解决方法:
最初,我们原本不想安装任何额外的插件来解决这个问题。然而,由于我们没有找到令人满意的解决方案,我们不得不尝试使用WP Extended Search插件。而它有效!
英文:
A SOLUTION WAS FOUND:
We originally did not want to install any additional plugins to solve this problem however, because we did not find a satisfactory solution we resorted to trying the WP Extended Search plugin. And it works!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论