The regular WordPress search returns all posts if no search term is entered. You’ll notice this when using the default search form. If you don’t want your AJAX search to behave this way then you need to check if a search term has been entered before doing anything else:
if ( ! empty( $_POST['keyword'] ) && $query->have_posts() ) {
Or better yet, just don’t send the AJAX request if there’s no search term:
var keyword = jQuery('#keyword').val();
if ( keyword ) {
jQuery.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'post',
data: {
action: 'data_fetch',
keyword: keyword
},
success: function(data) {
jQuery('#datafetch').html( data );
}
});
} else {
jQuery('#datafetch').html( '' );
}
Related Posts:
- How to display something in a div when the user clicks on a text in another div – no page refresh [closed]
- How to add a do_action on refreshing of WP customizer?
- Removing jQuery migrate and working with dependencies
- How to use wp_localize_script in a WordPress page other than functions.php?
- Real time Duplicate title check
- Load custom field value into div with AJAX
- Jquery ajax to custom php file: returning blank data
- Update WordPress Custom Field with AJAX on cached page
- Looking for a better way to handle an ajax script that pulls in post data on click
- How to add default images into theme customizer image control?
- Can’t use get_results() in ajax query
- WordPress Ajax Data problem
- Problem with WordPress Ajax form
- Display notification only once
- Access/update database with jQuery
- Passing jQuery value using Ajax to a page template
- ajax_object is not defined when ajax calls the function
- Returning a value from a PHP page
- Why is this Ajax not working?
- Stumped – Ajax Response Returns 0
- Colorbox ajax loading of outside HTML content works perfect on localhost but not on server
- Ajax return code 400
- doing an ajax request always outputs 0
- Woocommerce add to cart quantity buttons with AJAX
- class click counter save number
- AJAX request status 200 but no actual “response”
- Create another “Display Site Title and Tagline” checkbox, “Header Text Color” setting and control
- wordpress count link clicks by ip address
- ajax, right way to do it and make it works?
- Ajax load more posts with multiple tax query
- WordPress Sending data to Ajax with select option
- WordPress ajax doesn’t display object method on jQuery .change() function
- Ajax request not sending to server and returning – wp-admin/admin-ajax.php 400
- Save Search System
- populate form fields in a loop with ajax
- dynamic dependent select dropdown
- Menu jumping when calling it via PHP
- Setting value of session with Ajax not working
- Plugins not working on AJAX requests
- How to disable controls in theme customizer?
- How do I Import / Upload Files with jQuery AJAX?
- WordPress Ajax Call inserting data but success response false
- Ajax contact form return 0
- AJAX – Returning Two JSON Objects with One PHP Function
- Unset session variable on page reload / setup but exclude AJAX
- Add Ajax Hooks and Call from Custom Template PHP
- WordPress Ajax Always returning 0
- Function won’t run onclick using Ajax
- How to keep track of when a vote is cast?
- My jQuery Ajax form submit is still refreshing page?
- jQuery Ajax PHP function call returning [object Object]
- Output votes to div dynamically in wordpress
- Set cookie for visitor
- Execute Jquery when a specific page in my plugin is loading
- what does $options = get_option(‘test_theme’); do?
- How to load previous or next attachment with jquery ?
- Validating an email input from form field before submit using JQuery, AJAX, and PHP
- Customizer control save then return to default
- WordPress cascading dropdown selection search based on Parent Page & Child Page
- Ajax on the Administration Side of plugin – wp_localize_script – how to pass value from JQuery to PHP function in class?
- jquery & ajax getting data from php in wordpress . admin-ajax.php returns 400
- Failed to load resource: the server responded with a status of 500 (Internal Server Error) through wp_admin_ajax.php
- How do I call a value from the datase using ajax
- Ajax filter with loadmore button
- Where to add my PHP codes for AJAX Jquery to work?
- How to use wp_ajax_set_post_thumbnail?
- Update $wpdb query with AJAX
- How to reload a php script, using AJAX and jQuery
- AJAX to add to cart multiple products woocommerce
- wp_set_object_terms returns 500 error when called through AJAX function
- How can i send id of the currently posted form data through json response in custom wordpress plugin?
- Cannot successfully execute AJAX script to call function.php specific function. Using XAMPP localhost to test
- Remove specific javascript when viewing page in Customizer?
- How can i get the same ajax result using WP REST API instead of admin-ajax?
- Remove item from post_meta array via AJAX
- defined (‘ABSPATH’) false after AJAX post to other PHP-file
- AJAX Filter WHILE Loop not working WordPress
- Ajax not updating to database
- WordPress 404 when using AJAX to call php function
- wordpress ajax pagination
- how use php query in AJAX jquery
- Ajax Load more CPT via shortcode
- Why I’m Not Having Access to “$_POST” Data Outside My AJAX Callback?
- Submitting my form to the database and then redirecting to payment site
- A Customizer checkbox control that sets the setting to “” or to “blank” and show() or hide() a color control
- Pass the updated value of aid from form using ajax
- 500 (Internal Server Error) of external php file
- Use jQuery and AJAX to get HTML
- Dynamic Gallery
- best way to communicate between php and jquery/javascript
- Loading a post’s content, into a div outside the loop, when it’s title is selected in a list
- Fatal error: Uncaught Error: Call to undefined function wp_nav_menu()
- AJAX: WordPress filters inside $html do not work as intended
- Why not showing all post by default in my jquery filter
- Load Meta box value into div AJAX [duplicate]
- Jquery autosave text area after typing
- Pass a PHP variable (loop-audio.php) to jQuery function (js/script.js)
- notify users when changed database
- getJSON response to PHP
- ajax problems on loading page [closed]