12% off of LTD using this coupon: TWELVEPERCENTOFF. Promo ends on 2 Dec midnight UTC.
Published on Mar 15, 2021

How to Modify WP Grid Builder Query in Oxygen

Sridhar Katakam

WP Grid Builder (affiliate link) is a premium WordPress plugin for creating grids and facet filters.

One caveat with using pre_get_posts in a Code Block to modify an Oxygen‘s Easy Posts’ or Repeater’s secondary query as originally shared by Yan Kiara is that WP Grid Builder does not account for the updated dynamic query. This is because WPGB only takes into consideration the query set directly for Easy Posts or Repeater for performance reasons when using their Oxygen addon.

Sent the plugin developer, Loïc Blascos a support ticket, promptly got a working solution and I shall share the same in this article.

As an example, let us consider this scenario:

  • We want the posts to be filtered by categories on a static Page.
  • The desired query modification is to only show the posts that have been published by the currently logged-in user.
  • We are using a Repeater component to output the posts.

The usual process is to add a Repeater, set the query to custom, select post Post Type, add a Code Block immediately above the Repeater having this code:

<?php

add_action( 'pre_get_posts', 'wpdd_published_by_current_logged_in_user' );
function wpdd_published_by_current_logged_in_user( $query ) {
	remove_action( 'pre_get_posts', 'wpdd_published_by_current_logged_in_user' );

	$query->set( 'author', get_current_user_id() );
}

?>

While this does filter the posts on the front end to only those published by the currently logged-in user, the WPGB facet filters continue to show the same number of posts as before i.e., WP Grid Builder does not take the modified query into account. The reason for this is:

If you add custom PHP code to a page, it will not be executed during filtering.
By using the Oxygen add-on, performances are improved by only loading the content to be filtered in Ajax.

This means that it will not reload the entire page and it will not execute the code you added.

– Loïc Blascos

The solution is to add the pre_get_post code outside in a Code Snippets plugin or a custom functionality plugin just like how we would do for main queries AND target the correct query.

Remember: Since the list of posts is something that a static Page natively NOT has awareness of in WordPress (it only knows about that static Page’s content), we are dealing with a secondary query and not the main query here.

Screenshot of the solution shared by the developer (details follow):

Details

Install and activate Code Snippets plugin.

Go to Snippets > Add New.

Title: Modify WP Grid Builder Query

Code:

add_action(
	'pre_get_posts',
	function( $query ) {

		if ( 'oxygen-element-8' === $query->get( 'wp_grid_builder' ) ) {
			$query->set( 'author', get_current_user_id() );
		}
	},
	PHP_INT_MAX - 10
);

Set the snippet to run everywhere. Save changes and activate.

In oxygen-element-8, 8 is the “ID” of the Repeater as it appears in the Structure panel in the Oxygen editor.

With this in place, both the facet filters and the actual Repeater’s (or Easy Posts, if using that) posts now will respect the modified query set by your parameter(s),

$query->set( 'author', get_current_user_id() );

in this case.

tagschevron-leftchevron-rightchainangle-rightangle-upangle-downfolder-omagnifiercrossmenuchevron-downarrow-right