Reputation: 1
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!
Upvotes: 0
Views: 483
Reputation: 1
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!
Upvotes: 0