Meta Box Taxonomy Field’s Image Value in Posts

In a project I am currently working on, the requirement is to show the image of an “Image Advanced” type attached to a taxonomy in CPT posts in a Bricks site.

Taxonomy: Review Source (for Review CPT)
Custom field: Review Logo (Image Advanced with max. no. of files = 1)

CPT: Review
Custom field: Review Source (Taxonomy)

Objective: In the Review CPT query loop, show the review source’ logo.

Note that the taxonomy term is not the one that’s set for the post but rather, a custom field.

This Pro tutorial shows how to arrive at the necessary code step-by-step instead of just providing it so you can follow the same approach for your situation.

First, here’s the final code so you can see what we are arriving at:

<?php 

function bl_get_review_source_logo_id() {
    return array_keys( rwmb_meta( 'review_logo', [ 'object_type' => 'term', 'size' => 'full' ], rwmb_meta( 'review_source' )->term_id ) )[0];
}

The Image element of Bricks expects an attachment ID. So, we need to get the Review Logo’s ID.

Step 1

Add a Code element inside the Review CPT’s query loop.

As the “Theme Code” Meta Box in the Review Fields field group shows, the Review Source field’s term object can be obtained via:

<?php
// Getting selected term object:
$term = rwmb_meta( 'review_source' );
?>

Let’s see what data is contained in $term. Change that code to

<?php

// Getting selected term object:
$term = rwmb_meta( 'review_source' );

print( "<pre>" . print_r( $term, true ) . "</pre>" );

?>

Step 2

Looking at the field group attached to Review Source taxonomy, Meta Box provides this handy theme code:

<?php
// Displaying uploaded images:
$images = rwmb_meta( 'review_logo', [ 'object_type' => 'term', 'size' => 'thumbnail' ], get_queried_object_id() );
?>

The last argument of rwmb_meta() function is the current term’s ID.

As seen from the printed data in Step 1, this can be obtained by $term->term_id.

To get a property of a PHP object, the syntax to be used is object->property.

So, our full code now becomes:

<?php

// Getting selected term object:
$term = rwmb_meta( 'review_source' );

print( "<pre>" . print_r( $term, true ) . "</pre>" );

// Displaying uploaded images:
$images = rwmb_meta( 'review_logo', [ 'object_type' => 'term', 'size' => 'thumbnail' ], $term->term_id );

?>

In this particular example, we need full-sized images. Let’s examine $images. Change the code to:

<?php

// Getting selected term object:
$term = rwmb_meta( 'review_source' );

// print( "<pre>" . print_r( $term, true ) . "</pre>" );

// Displaying uploaded images:
$images = rwmb_meta( 'review_logo', [ 'object_type' => 'term', 'size' => 'full' ], $term->term_id );

print( "<pre>" . print_r( $images, true ) . "</pre>" );

?>

As seen from the above, $images array is not a simple indexed array (where the key starts with 0) but rather an associative array with a key => value pair. The keys (1012, 921) are what we want to pull. These are the IDs of the Review Logo images.

PHP has a function called array_keys(), which returns a new array with the keys of the supplied array.

Change the code to:

<?php

// Getting selected term object:
$term = rwmb_meta( 'review_source' );

// print( "<pre>" . print_r( $term, true ) . "</pre>" );

// Displaying uploaded images:
$images = rwmb_meta( 'review_logo', [ 'object_type' => 'term', 'size' => 'full' ], $term->term_id );

// print( "<pre>" . print_r( $images, true ) . "</pre>" );

print( "<pre>" . print_r( array_keys( $images ), true ) . "</pre>" );

?>

The gold we are digging for is the first element of the array. Change the code to:

<?php

// Getting selected term object:
$term = rwmb_meta( 'review_source' );

// print( "<pre>" . print_r( $term, true ) . "</pre>" );

// Displaying uploaded images:
$images = rwmb_meta( 'review_logo', [ 'object_type' => 'term', 'size' => 'full' ], $term->term_id );

// print( "<pre>" . print_r( $images, true ) . "</pre>" );

// print( "<pre>" . print_r( array_keys( $images ), true ) . "</pre>" );

print( "<pre>" . print_r( array_keys( $images )[0], true ) . "</pre>" );

?>

Now that we got what we want, let’s go from bottom to top and expand it out.

array_keys( $images )[0]

would be

array_keys( rwmb_meta( 'review_logo', [ 'object_type' => 'term', 'size' => 'full' ], $term->term_id ) )[0]

and that would be

array_keys( rwmb_meta( 'review_logo', [ 'object_type' => 'term', 'size' => 'full' ], rwmb_meta( 'review_source' )->term_id ) )[0]

Step 3

Create a custom function having:

<?php 

function bl_get_review_source_logo_id() {
    return array_keys( rwmb_meta( 'review_logo', [ 'object_type' => 'term', 'size' => 'full' ], rwmb_meta( 'review_source' )->term_id ) )[0];
}

In the Bricks editor, add an Image element and set its source to:

{echo:bl_get_review_source_logo_id}