/grid/query_args

DescriptionDescription

This filter is called before the grid query is ran to fetch posts, terms or users.

It allows to dynamically change the query args of WP_Query , WP_Term_Query or WP_User_Query.

This filter is only called for grids built with the plugin and will not work with custom and archive queries. In these cases, you need to use pre_get_posts action of WordPress to modify a query.

ArgumentsArguments

ArgumentTypeDescription
$query_args arrayHolds query arguments
$grid_idintegerGrid id

ExamplesExamples

Exclude current postExclude current post

The following example demonstrates how to exclude the current post from a grid. Since a grid can be queried during Ajax requests (filtering and rendering of facets), we need to get the current post ID from the referrer instead of the WordPress global.

PHP
functions.php
function grid_query_exclude_current_post( $query_args, $grid_id ) {

	global $post;

	// If it matches grid id 1.
	if ( 1 === $grid_id ) {

		// The grid is also queried during Ajax requests when filtering or rendering facets.
		// So we have to get the ID of the current post/page thanks to the referer.
		$referer = wp_get_referer();
		$post_id = wp_doing_ajax() ? url_to_postid( $referer ) : $post->ID;

		$query_args['post__not_in'] = [ $post_id ];

	}

	return $query_args;

}
add_filter( 'wp_grid_builder/grid/query_args', 'grid_query_exclude_current_post', 10, 2 );

Include postsInclude posts

The following example demonstrates how to include posts IDs, stored in a custom field, thanks to the post__in clause of WP_Query.

PHP
functions.php
function grid_query_include_post_ids( $query_args, $grid_id ) {

	global $post;

	// If it matches grid id 1.
	if ( 1 === $grid_id ) {

		// The grid is also queried during Ajax requests when filtering or rendering facets.
		// So we have to get the ID of the current post/page thanks to the referer.
		$referer = wp_get_referer();
		$post_id = wp_doing_ajax() ? url_to_postid( $referer ) : $post->ID;

		// You have to change "custom_field_name" by yours.
		$query_args['post__in'] = (array) get_post_meta( $post_id, 'custom_field_name', true );

	}

	return $query_args;

}
add_filter( 'wp_grid_builder/grid/query_args', 'grid_query_include_post_ids', 10, 2 );

Related postsRelated posts

The following example demonstrates how to query related posts from an associated taxonomy in single post pages.

PHP
functions.php
function grid_query_related_posts( $query_args, $grid_id ) {

	global $post;

	// If it does not match grid id 1.
	if ( 1 !== $grid_id ) {
		return $query_args;
	}

	$taxonomy = 'category'; // You have to change "category" to the associated taxonomy.
	$post_id  = wp_doing_ajax() ? url_to_postid( wp_get_referer() ) : $post->ID;
	$terms    = get_the_terms( $post_id, $taxonomy );

	if ( is_wp_error( $terms ) || empty( $terms ) ) {
		$query_args['post__in'] = [ 0 ];
	} else {

		$query_args['post__not_in'] = [ $post_id ];
		$query_args['tax_query']    = [
			[
				'taxonomy' => $taxonomy,
				'terms'    => wp_list_pluck( $terms, 'term_id' ),
			],
		];
	}

	return $query_args;

}
add_filter( 'wp_grid_builder/grid/query_args', 'grid_query_related_posts', 10, 2 );

Related productsRelated products

The following example demonstrates how to query related WooCommerce products.

PHP
functions.php
function grid_query_related_products( $query_args, $grid_id ) {

	global $post;

	// If it does not match grid id 1.
	if ( 1 !== $grid_id || ! function_exists( 'wc_get_product' ) ) {
		return $query_args;
	}

	$referer = wp_get_referer();
	$post_id = wp_doing_ajax() ? url_to_postid( $referer ) : $post->ID;
	$product = wc_get_product( $post_id );

	if ( empty( $product ) ) {
		$product_ids = [];
	} else {
		$posts_per_page = isset( $query_args['posts_per_page'] ) ? $query_args['posts_per_page'] : 4;
		$product_ids    = wc_get_related_products( $post_id, $posts_per_page, $product->get_upsell_ids() );
	}

	$query_args['post_type'] = [ 'product' ];
	$query_args['post__in']  = empty( $product_ids ) ? [ 0 ] : $product_ids;

	return $query_args;

}
add_filter( 'wp_grid_builder/grid/query_args', 'grid_query_related_products', 10, 2 );

Cross-sell productsCross-sell products

The following example demonstrates how to query cross-sell WooCommerce products.

PHP
functions.php
function grid_query_cross_sell_products( $query_args, $grid_id ) {

	global $post;

	// If it does not match grid id 1.
	if ( 1 !== $grid_id || ! function_exists( 'wc_get_product' ) ) {
		return $query_args;
	}

	$referer = wp_get_referer();
	$post_id = wp_doing_ajax() ? url_to_postid( $referer ) : $post->ID;
	$product = wc_get_product( $post_id );

	if ( empty( $product ) ) {
		$product_ids = [];
	} else {
		$product_ids = $product->get_cross_sell_ids();
	}

	$query_args['post_type'] = [ 'product' ];
	$query_args['post__in']  = empty( $product_ids ) ? [ 0 ] : $product_ids;

	return $query_args;

}
add_filter( 'wp_grid_builder/grid/query_args', 'grid_query_cross_sell_products', 10, 2 );

Upsell productsUpsell products

The following example demonstrates how to query upsell WooCommerce products.

PHP
functions.php
function grid_query_upsell_products( $query_args, $grid_id ) {

	global $post;

	// If it does not match grid id 1.
	if ( 1 !== $grid_id || ! function_exists( 'wc_get_product' ) ) {
		return $query_args;
	}

	$referer = wp_get_referer();
	$post_id = wp_doing_ajax() ? url_to_postid( $referer ) : $post->ID;
	$product = wc_get_product( $post_id );

	if ( empty( $product ) ) {
		$product_ids = [];
	} else {
		$product_ids = $product->get_upsell_ids();
	}

	$query_args['post_type'] = [ 'product' ];
	$query_args['post__in']  = empty( $product_ids ) ? [ 0 ] : $product_ids;

	return $query_args;

}
add_filter( 'wp_grid_builder/grid/query_args', 'grid_query_upsell_products', 10, 2 );

On Sale productsOn Sale products

The following example demonstrates how to query on sale WooCommerce products.

PHP
functions.php
function grid_query_on_sale_products( $query_args, $grid_id ) {

	// If it matches grid id 1.
	if ( 1 === $grid_id && function_exists( 'wc_get_product_ids_on_sale' ) ) {
		$query_args['post__in'] = wc_get_product_ids_on_sale();
	}

	return $query_args;

}
add_filter( 'wp_grid_builder/grid/query_args', 'grid_query_on_sale_products', 10, 2 );