Skip to content

Instantly share code, notes, and snippets.

@mgibbs189
Created April 20, 2016 01:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgibbs189/f2469009a7039159e229efe5a01dab23 to your computer and use it in GitHub Desktop.
Save mgibbs189/f2469009a7039159e229efe5a01dab23 to your computer and use it in GitHub Desktop.
FacetWP - add a "Load more" button below results
<?php
function fwp_load_more() {
?>
<script>
(function($) {
$(function() {
if ('object' != typeof FWP) {
return;
}
wp.hooks.addFilter('facetwp/template_html', function(resp, params) {
if (FWP.is_load_more) {
FWP.is_load_more = false;
$('.facetwp-template').append(params.html);
return true;
}
return resp;
});
$(document).on('click', '.fwp-load-more', function() {
$('.fwp-load-more').html('Loading...');
FWP.is_load_more = true;
FWP.paged = parseInt(FWP.settings.pager.page) + 1;
FWP.soft_refresh = true;
FWP.refresh();
});
$(document).on('facetwp-loaded', function() {
if (FWP.settings.pager.page < FWP.settings.pager.total_pages) {
if (! FWP.loaded && 1 > $('.fwp-load-more').length) {
$('.facetwp-template').after('<button class="fwp-load-more">Load more</button>');
}
else {
$('.fwp-load-more').html('Load more').show();
}
}
else {
$('.fwp-load-more').hide();
}
});
});
})(jQuery);
</script>
<?php
}
add_action( 'wp_head', 'fwp_load_more', 99 );
@PierreBoislard3
Copy link

This code work great except when you refresh the browser. If you display 5 results per page and you click 2 times on the 'load more' button, you will see 15 results as expected, but if you refresh the browser, you will now see only 5 results and those results are from 10 to 15. First 10 results are ignored because url hash is set to ?fwp_paged=3

@Pamps
Copy link

Pamps commented Aug 17, 2017

Here's a solution to remove the fwp_paged query string param and preventing the jump down the page

// Remove the WPFacet fwp_paged query string parameter on load
// Useful for when loading WPFacet entries dynamically
function hlp_fah_remove_query_param()
{
	
	if ( isset($_GET['fwp_paged']) ) {
		
		// Get the current URL		
		$current_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
		
		// Remove the param
		$current_url = remove_query_arg('fwp_paged', $current_url);
		
		// Visit the new URL
		wp_redirect( $current_url );
		exit();
		
	}
	
}
add_action( 'template_redirect', 'hlp_fah_remove_query_param' );

@l364cyv1
Copy link

Instead of removing query parameter you can force facetwp template load with this one line:

add_filter( 'facetwp_template_force_load', '__return_true' );

@nboaldin
Copy link

nboaldin commented Jan 8, 2019

I don't know how relevant this is, today. But I came across this code in a project, and it was breaking FacetWP. In line 12 wp.hooks should be FWP.hooks

Thank you Wordpress 5.0

@yannickninot
Copy link

No more need to write code to implement a load more button. The facetwp plugin added this functionality. You can just check this option in the plugin settings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment