/grid/the_object

DescriptionDescription

This filter is called when an object (post, term or user) has been setup in the loop after the WP_Query, WP_Term_Query or WP_User_Query.

It allows to modifies object properties like the title, excerpt, etc…

ArgumentsArguments

ArgumentTypeDescription
$objectobjectHolds current object data in the loop (title, excerpt, etc…)

ExamplesExamples

Custom thumbnailCustom thumbnail

The following example demonstrates how to display a custom image in a card from a custom field that stores the image ID. This code works with ACF or Meta Box image field (not from a repeater/clone/group). It will also work with any custom fields plugin that stores the image ID.

PHP
functions.php
function set_custom_card_thumbnail( $object ) {

	// Get settings of the current grid.
	$grid = wpgb_get_grid_settings();

	// If it does not match the grid id 1.
	if ( 1 !== $grid->id ) {
		return $object;
	}

	// You have to change "custom_field_name" by yours.
	$image_id = get_post_meta( $object->ID, 'custom_field_name', true );

	if ( ! empty( $image_id ) ) {
		$object->post_thumbnail = $image_id;
	}

	return $object;

}
add_filter( 'wp_grid_builder/grid/the_object', 'set_custom_card_thumbnail' );

Custom galleryCustom gallery

The following example demonstrates how to display a custom gallery in a card from a custom field that stores the image IDs. This code works with ACF or Meta Box gallery field (not from a repeater/clone/group). It will also work with any custom fields plugin that stores the image IDs.

PHP
functions.php
function set_custom_card_gallery( $object ) {

	// Get settings of the current grid.
	$grid = wpgb_get_grid_settings();

	// If it does not match the grid id 1.
	if ( 1 !== $grid->id ) {
		return $object;
	}

	// You have to change "custom_field_name" by yours.
	$image_ids = get_post_meta( $object->ID, 'custom_field_name', true );

	if ( empty( $image_ids ) ) {
		return $object;
	}

	$object->post_format = 'gallery';
	$object->post_media  = [
		'format'  => 'gallery',
		'sources' => $image_ids,
	];

	// Sets post thumbnail from the first image in the gallery.
	$object->post_thumbnail = reset( $image_ids );

	return $object;

}
add_filter( 'wp_grid_builder/grid/the_object', 'set_custom_card_gallery' );

Custom videoCustom video

The following example demonstrates how to display a custom video in a card from a custom field that stores the video URL (hosted video).

PHP
functions.php
function set_custom_card_video( $object ) {

	// Get settings of the current grid.
	$grid = wpgb_get_grid_settings();

	// If it does not match the grid id 1.
	if ( 1 !== $grid->id ) {
		return $object;
	}

	// You have to change "custom_field_name" by yours.
	$video_url = get_post_meta( $object->ID, 'custom_field_name', true );

	if ( empty( $video_url ) ) {
		return $post;
	}

	$object->post_format = 'video';
	$object->post_media  = [
		'format'  => 'video',
		'type'    => 'hosted',
		'sources' => [ $video_url ],
	];

	return $object;

}
add_filter( 'wp_grid_builder/grid/the_object', 'set_custom_card_video' );