Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Override WordPress search page using CloudSearch plugin
<?php
// Header
get_header();
// Get search keyword
$keyword = get_search_query();
// Prepare filter query
$filter_query = '';
?>
<section>
<div>
<span>You are looking for</span>
<h1><?php echo $keyword ?></h1>
<ul>
<li>
<span>
total results <span id="acs_found_items"></span>
</span>
</li>
<li>
sort by:
<div>
<select name="orderby" id="orderby">
<option value="post_date_gmt">Date</option>
<option value="_score">Relevance</option>
</select>
</div>
</li>
</ul>
</div>
<div>
<div class="acs_search_results_container" id="acs_search_results_container">
<div class="acs_search_results_items" id="acs_search_results_items">
<!-- Result items -->
</div>
<div class="load-more loading">
<a class="btn acs-more" style="visibility: hidden;"><?php echo ACS::get_instance()->get_settings()->acs_results_load_more_msg ?></a>
<div class="loading-spinner acs-loader" style="display: none;"></div>
</div>
</div>
</div>
</section>
<script type="text/javascript">
jQuery(document).ready(function() {
// Initialize search manager
var start = 0;
var keyword = '<?php echo $keyword ?>';
var filter_query = '<?php echo $filter_query ?>';
var filter_type_field = 'all';
var filter_sort_field = 'post_date_gmt';
var filter_sort_order = 'desc';
var container_result_items = '#acs_search_results_items';
var container_load_more = '#acs_search_results_container .acs-more';
var container_ajax_loader = '#acs_search_results_container .acs-loader';
var container_count_items = '#acs_found_items';
// Add load more event listener
jQuery(container_load_more).click(function(e) {
// Prevent defaults
e.preventDefault();
// Perform new search
acs_search();
});
// Perform initial search
acs_search();
// Add sort field filter change event listener
jQuery('#orderby').change(function(e) {
// Prevent defaults
e.preventDefault();
// Perform a reset and new search
acs_reset();
acs_search();
});
function acs_reset() {
// Reset results and pagination value
jQuery(container_result_items).html('');
// Remove 'no_results' CSS class
jQuery(container_result_items).removeClass('no_results');
// Reset start position
start = 0;
}
function acs_search() {
// Hide load more and show AJAX loader
jQuery(container_load_more).css('visibility', 'hidden');
jQuery(container_ajax_loader).show();
// Get sort values
filter_sort_field = jQuery('#orderby').val();
jQuery.ajax({
url: '/wp-admin/admin-ajax.php',
type: 'get',
dataType: 'json',
data: {
action: 'acs_search_documents_full',
keyword: keyword,
start: start,
size: <?php echo ACS::get_instance()->get_settings()->acs_results_max_items ?>,
filter_query: filter_query,
type_field: filter_type_field,
sort_field: filter_sort_field,
sort_order: filter_sort_order,
extras: {
mobile: 0
}
},
success: function (result) {
// Update start point
start = result.data.start;
// Hide AJAX loader
jQuery(container_ajax_loader).hide();
if (result.data.load_more) {
// Show load more if there are other results
jQuery(container_load_more).css('visibility', 'visible');
}
else {
// Hide load more
jQuery(container_load_more).css('visibility', 'hidden');
}
if (result.data.found > 0) {
// Append search results
jQuery(container_result_items).append(result.data.items);
}
else {
// No results found
jQuery(container_result_items).append(result.data.message);
// Add 'no_results' CSS class
jQuery(container_result_items).addClass('no_results');
}
// Update items found
jQuery(container_count_items).html(result.data.found);
}
});
}
});
</script>
<?php
// Footer
get_footer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.