WP Grid Builder - Remove sticky posts from filtered results upon direct access

Hi,

I was building a site that required sticky posts to show on top of the results, then have alphabetical sorting afterwards. This works natively with the Bricks query loop, and when filtering with WPGB the sticky posts get filtered out as usual.

However, when accessing a filtered URL directly (by link or by just refreshing the page) the sticky posts would show up on top of the fitlered results. This was an issue for the site I’m working on, so I reached out to their support team for guidance. They provided me with a code snippet that will remove sticky posts from filtered results. I thought I would kindly share this here, in case anyone might need it in the future. Here is the code snippet:

add_filter(
	'bricks/posts/query_vars',
	function( $query_vars, $settings, $element_id ) {

		if ( ! empty( $query_vars['wpgb_bricks'] ) && ! empty( wpgb_get_url_search_params() ) ) {
			$query_vars['ignore_sticky_posts'] = true;
		}

		return $query_vars;

	},
	PHP_INT_MAX - 9,
	3
);

Remember to specify which Bricks element, so it’s not applied to every loop:

if ( $element_id == 'fhmnfx')

Due to time constraints, we ended up not using the sticky system, and used an automatically filled hidden custom field to allow for custom orderby parameters instead. But, this code is invaluable, and WPGB doesn’t have a forum for discussion/support.

2 Likes