FacetWP – “Load more” support for Beaver Builder’s post grid module

<?php

/**
 * "Load more" works by appending new content into .facetwp-template
 * 
 * The problem here is that the actual results are several elements deep, so "Load more" is
 * appending unnecessary container elements and breaking the layout.
 
 * The code below cherry-picks the new content from ".pp-content-post-grid" and injects it
 * into the existing ".pp-content-post-grid" container element.
 */

add_action( 'wp_footer', function() {
?>
<script>
(function($) {
    FWP.hooks.addFilter('facetwp/template_html', function(resp, params) {
        if (FWP.is_load_more) {
            FWP.is_load_more = false;
            var html = $(params.html).find('.pp-content-post-grid').html();
            $('.facetwp-template .pp-content-post-grid').append(html);
            return true;
        }
        return resp;
    }, 8);
})(jQuery);
</script>
<?php
}, 100 );