Ajax search – remove search results if the srch field is empty + excluding pages
-
Hey there,
basically I have an ajax search that displays my website’s current pages while typing for a specific keyword -BUT- once I clear the search box, all of the pages remain there and won’t get cleared.Also do you guys have any idea how can I exclude pages from being searched with my current code?
Thank you all for the help.functions.php
function data_fetch(){ $the_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => 'page' ) ); if( $the_query->have_posts() ) : while( $the_query->have_posts() ): $the_query->the_post(); ?> <h2><a href="<?php echo esc_url( get_permalink() ); ?>"><?php the_title();?></a></h2> <?php endwhile; wp_reset_postdata(); endif; die(); }
Ajax
function fetch(){ jQuery.ajax({ url: "mywebsite/wp-admin/admin-ajax.php", type : "POST", data: { action: 'data_fetch', keyword: jQuery('#keyword').val() }, success: function(data) { if('#keyword'=="") { jQuery('#datafetch').html(""); return; } else { jQuery('#datafetch').html( data ); } } }); };
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘Ajax search – remove search results if the srch field is empty + excluding pages’ is closed to new replies.