Question about hooking into Posts module query for facetwp

Hey beavers,

Question: is there a way to hook into the post module query and add extra arguments without cloning and editing the module or core files?

I’m using facetwp for faceted search. They said you can use it with existing layouts and I’d love to use the bb module layouts in the post grid module.

They said it won’t detect the query by default so you need to do this:
https://facetwp.com/using-facetwp-with-existing-template-files/

I’ve set up the hook described in my functions.php file, added the class ‘facetwp-template’ to the posts module in the class field and have tried to clone the module into my theme per the instructions and place the recommended query argument in the post module’s frontend.php settings and just default it to true. For example, I tried this:

	'content'   => array(
		'title'         => __('Content', 'fl-builder'),
		'file'          => FL_BUILDER_DIR . 'includes/loop-settings.php',
	        'facetwp' => true

I even tried to put it on the core loop-settings.php file but still no luck.I keep getting

FacetWP was unable to auto-detect the post listing

I realize this is out of scope so no worries if you can’t help but would love any insight if you can.

Thanks for everything!

Hey Thomas,

I’ve already assigned another member of the team to check in on this.

Ben

Hey Thomas,

Could you use the pre_get_posts filter to set that query var? Have a look at that and let me know what you think.

Justin

Update: I saw that I could add this to the class-fl-builder-loop.php file in the args array and it will render on the page.

		// Build the query args.
		$args = array(
			'paged'					=> $paged,
			'posts_per_page'		=> $posts_per_page,
			'post_type'				=> $post_type,
			'orderby'				=> $order_by,
			'order'					=> $order,
			'author'				=> $users,
			'tax_query'				=> array('relation' => 'AND'),
			'ignore_sticky_posts'	=> true,
			'offset'				=> $paged_offset,
			'fl_original_offset'	=> $offset,
			'fl_builder_loop'		=> true,
			'fields'				=> $fields,
			'facetwp' => true
		);

However, when a facet is selected, everything disappears and obviously this is not a good method. hopefully there’s some way to do this.

Hi Justin, thanks for the help!

hmm… that pre_get_posts filter sort of works:

function search_filter($query) {
 
      $query->set('facetwp', true);

}

it displays the posts from the posts module and doesn’t throw the “FacetWP was unable to auto-detect the post listing” error. However, it also doesn’t show the facets on the page.

When I update the args in the class-fl-builder-loop.php file as noted in the earlier thread post, it pretty much works: it shows the facets and the intial post results. However, when choosing a facet, it removes the post’s inline-style that makes it hidden. Not sure why, still debugging.

Here’s the class when I first load the page:

<div class="fl-post-grid-post masonry-brick" itemscope="itemscope" itemtype="http://schema.org/BlogPosting" style="position: absolute; left: 0px; top: 0px; visibility: visible;>

Here is after I select a facet:

<div class="fl-post-grid-post masonry-brick" itemscope="itemscope" itemtype="http://schema.org/BlogPosting">

Let me know if I’m using the pre_get_posts in correctly.

When I update the args in the class-fl-builder-loop.php file as noted in the earlier thread post, it pretty much works

Would it help if we added a filter to those args so you could add the facet param there instead? Something like this…

$args = apply_filters( 'fl_builder_loop_query_args', array(
	'paged'					=> $paged,
	'posts_per_page'		=> $posts_per_page,
	'post_type'				=> $post_type,
	'orderby'				=> $order_by,
	'order'					=> $order,
	'author'				=> $users,
	'tax_query'				=> array('relation' => 'AND'),
	'ignore_sticky_posts'	=> true,
	'offset'				=> $paged_offset,
	'fl_original_offset'	=> $offset,
	'fl_builder_loop'		=> true,
	'fields'				=> $fields
) );

As for the second issue, that might be something you’ll have to ask them about. It sounds like they might have some JS that is doing that.

Justin

Yes! A Filter would definitely help!

I found the code that puts the visibility in the frontend.js file for the posts module:

Any idea why it does that? I could remove it but want to be sure it’s not going to eat me alive. nom nom.

	FLBuilderPostGrid.prototype = {
	
		settings        : {},
		nodeClass       : '',
		wrapperClass    : '',
		postClass       : '',
		gallery         : null,
		
		_hasPosts: function()
		{
			return $(this.postClass).length > 0;
		},
		
		_initLayout: function()
		{
			switch(this.settings.layout) {
				
				case 'grid':
				this._gridLayout();
				break;
				
				case 'gallery':
				this._galleryLayout();
				break;
			}
			
			$(this.postClass).css('visibility', 'visible');
		},

update:

When I just change the css from hidden to visible,

.fl-post-gallery-post {
	overflow: hidden;
	position: relative;
	visibility: visible;
}

I see everything on facet selection. However, it’s doing something odd. Posting a link to it in my next comment. Let me know if you know what’s going on.

[Content Hidden]

Right now, I’m still hacking the core file to get the facetwp argument in the loop (until the filter comes out). However, I fixed the content not showing up after a facet is checked.

I just had to hook into facetwp-loaded action: https://facetwp.com/documentation/facetwp-loaded/

I put this in my frontend.js inside the posts module (in my theme) inside the function FLBuilderPostGrid.

$(document).on('facetwp-loaded', function() { self._initLayout(); });

This reinitializes the masonry-brick layout when any facet is triggered.

I still can’t get the pagination to work. Facetwp has their own pagination and so does the posts module. If you have any suggestions, I much appreciate it!

Thanks, Justin!

Right now, I’m still hacking the core file to get the facetwp argument in the loop (until the filter comes out).

No problem there. I’ve just pushed a commit with that filter so it will be in the next release.

I still can’t get the pagination to work. Facetwp has their own pagination and so does the posts module. If you have any suggestions, I much appreciate it!

Do you need to replace the post module’s pagination with theirs? If so, I have some ideas there.

Justin

yes, I do since they seem to use their own flavor of pagination.

What are your thoughts?

Great! I’m thinking another filter is in order that will let you override the markup for the layout without having to override the entire module. That has actually been a fairly popular request, so we should get that done. Actually, it is :slight_smile: I just pushed a commit that adds the fl_builder_posts_module_layout_path filter. To use that now, change this in the post module’s frontend.php file…

include $module->dir . 'includes/post-' . $settings->layout . '.php';

to this…

include apply_filters( 'fl_builder_posts_module_layout_path', $module->dir . 'includes/post-' . $settings->layout . '.php', $settings->layout );

You can then use that filter to point the path to your own copy of the layout.

Let me know what you think about that and if you have any questions.

Justin

Hey, just checking on this filter. Any idea when it will be released?

Specifically, I’m looking to insert extra queries into the loop for the posts layout module without hacking into the class-fl-builder-loop.php file.

Thanks!

Hey Thomas,

The fl_builder_posts_module_layout_path filter and fl_builder_loop_query_args filter were released in 1.7.6 earlier this week. Have you upgraded to that yet?

Justin