在WordPress中的AJAX搜索结果中包括标签和分类。

huangapple go评论70阅读模式
英文:

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( &#39;wp_footer&#39;, &#39;ajax_fetch&#39; );
function ajax_fetch() {
?&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
function fetch(){

    jQuery.ajax({
        url: &#39;&lt;?php echo admin_url(&#39;admin-ajax.php&#39;); ?&gt;&#39;,
        type: &#39;post&#39;,
        data: { action: &#39;data_fetch&#39;, keyword: jQuery(&#39;#keyword&#39;).val() },
        success: function(data) {
            jQuery(&#39;#datafetch&#39;).html( data );
        }
    });

}
&lt;/script&gt;
&lt;?php
}

// the ajax function
add_action(&#39;wp_ajax_data_fetch&#39; , &#39;data_fetch&#39;);
add_action(&#39;wp_ajax_nopriv_data_fetch&#39;,&#39;data_fetch&#39;);
function data_fetch(){

    $the_query = new WP_Query( array( &#39;posts_per_page&#39; =&gt; 8, &#39;s&#39; =&gt; esc_attr( $_POST[&#39;keyword&#39;] ), &#39;post_type&#39; =&gt; array(&#39;post&#39;) ) );
    if( $the_query-&gt;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.

&lt;script type=&quot;text/javascript&quot;&gt;
function fetch(){

    jQuery.ajax({
        url: &#39;&lt;?php echo admin_url(&#39;admin-ajax.php&#39;); ?&gt;&#39;,
        type: &#39;post&#39;,
        data: { action: &#39;data_fetch&#39;, keyword: jQuery(&#39;#keyword&#39;).val() },
        success: function(data) {
            jQuery(&#39;#datafetch&#39;).html( data );
        }
    });

}
&lt;/script&gt;

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!

huangapple
  • 本文由 发表于 2023年3月9日 18:50:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定