Created
May 17, 2023 05:08
-
-
Save bagerathan/f90ebbde47187de536961dd156ad6920 to your computer and use it in GitHub Desktop.
[Ajax search] Search custom post type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form role="search" method="post" class="search-form padding-4" action="<?php echo home_url( '/' ); ?>"> | |
<label> | |
<span class="screen-reader-text"><?php echo _x( 'Search for:', 'label' ) ?></span> | |
<input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Search...', 'placeholder' ) ?>" value="<?php echo get_search_query() ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" /> | |
<input class="post_type" type="hidden" name="post_type" value="frequent" /> | |
</label> | |
<input type="submit" class="search-submit button brand" id="searchsubmit" value="<?php echo esc_attr_x( 'Search', 'submit button' ) ?>" /> | |
</form> | |
<script type="text/javascript"> | |
jQuery(document).ready(function($){ | |
// Bind the submit event for your form | |
$('.search-form').submit(function( e ){ | |
// Stop the form from submitting | |
e.preventDefault(); | |
// Get the search term | |
var term = $('.search-field').val(); | |
// Get post type | |
var pt = $('.post_type').val(); | |
// Make sure the user searched for something | |
if ( term ){ | |
$.get( '/', { s: term, post_type: pt }, function( data ){ | |
// Place the fetched results inside the #content element | |
$('#inner-content').html( $(data).find('#inner-content') ); | |
}); | |
} | |
}); | |
}); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function template_chooser($template) { | |
global $wp_query; | |
$post_type = get_query_var('post_type'); | |
if( $wp_query->is_search && $post_type == 'frequent' ) { // Change post type here <-- | |
return locate_template('faq-search.php'); // redirect to archive-search.php | |
} | |
return $template; | |
} | |
add_filter('template_include', 'template_chooser'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment