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?
- How can I fetch loop of post titles via AJAX?
- Retrieve POST data from AJAX call
- Do I require the use of nonce?
- WordPress Ajax always returns a 404 error
- Removing jQuery migrate and working with dependencies
- problem with ajax and the path to the php page
- 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
- Security – Ajax and Nonce use [closed]
- 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
- Targeting single page with JS
- Infinite-Scroll Plugin and Jetpack Infinite Scroll Plugin – Adding to “Thoughts” Theme
- 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
- Click loads template via ajax
- Display notification only once
- How to retrieve the content (with a specific ID) via ajax by clicking a link tag
- Access/update database with jQuery
- Passing jQuery value using Ajax to a page template
- ajax_object is not defined when ajax calls the function
- How to speed up admin-ajax.php in wordpress
- 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
- Sending jsPDF documents to the server
- Use ajax from function.php
- Ajax return code 400
- wp_insert_post() is returning the correct post ID, no failure, but the post content does not get updated
- doing an ajax request always outputs 0
- How to test nonce with AJAX – Plugin development
- Woocommerce add to cart quantity buttons with AJAX
- class click counter save number
- Checking for new message using AJAX and PHP. Server overload?
- 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 loaded form replaces form action with Ajax url
- prevent default not stopping page refresh. Passing form information to and from php with ajax in a wordpress site
- Ajax date_query not working past the first iteration
- Ajax request not sending to server and returning – wp-admin/admin-ajax.php 400
- Why ajax doesn’t work on certain wordpress hooks and reload the page instead?
- AJAX pagination, update current page
- Save Search System
- adjust section according to country?
- populate form fields in a loop with ajax
- dynamic dependent select dropdown
- Change “add to cart” button color based on Woocommerce product category [closed]
- Ajax request returns ‘Array’. How to output the actual results?
- Menu jumping when calling it via PHP
- Setting value of session with Ajax not working
- [jQuery]Adding shortcode
- Display parent and child taxonomies in separate drop down select fields
- Show success or error messages in Ajax response to WordPress custom registration form
- How to add a Custom Mailchimp AJAX Newsletter Subscribe Form
- Plugins not working on AJAX requests
- Fancybox type popup window that’s not an iframe
- How to disable controls in theme customizer?
- Can I use a jQuery Ajax request in Code Snippets Plugin for WordPress?
- How do I Import / Upload Files with jQuery AJAX?
- WordPress admin-ajax.php 400 bad request
- WordPress Ajax Call inserting data but success response false
- Ajax contact form return 0
- AJAX – Returning Two JSON Objects with One PHP Function
- Pull GetOption() variable into jQuery dynamically created html
- 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?
- WordPress plugin admin page – using WordPress function in linked php file
- A $_POST should occur when submit form but is not?
- My jQuery Ajax form submit is still refreshing page?
- jQuery Ajax PHP function call returning [object Object]
- Shortcode to pull posts
- Why i can’t get custom fields value or post ID via Ajax?
- Find the method which AJAX GET calls
- AJAX Filter WHILE Loop not working WordPress
- Ajax not updating to database
- WordPress 404 when using AJAX to call php function
- wordpress ajax pagination
- WordPress AJAX load post metadata in modal
- 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
- ajax form with multiple submit buttons and values