Pods Relationship Field in Bricks

In this Pro tutorial, we explore the Relationship field of Pods and share ways to output items of a related post type incl. the associated custom field’s value in Bricks.

Custom Post Types: company and job.

Field for company CPT: company_location (Type: Plain Text).

Single Select

Field for job CPT: related_company (Type: Relationship)

Objective: On the job CPT’s singular pages, show the related company data like the title, the location custom field’s value and the content.

Create and edit “Single Job” template with Bricks.

Set a template condition to ensure that it applies to singular items of the job post type.

Add a Section having the Post Title and Post Content elements.

Add another Section having a “Related Company” heading.

Below the heading, add a Posts element or a Query Loop to output posts of the company post type.

Add Post Title, Basic Text and Post Content elements (query loop was used in our test site).

Change the Basic Text element’s text to:

Headquarters: {echo:pods_field_display(company_location)}

You may want to ensure that this elements gets output only if the field is not empty.

For this, add a Dynamic data condition like this:

Next, we need to limit the list of companies to one that is related to the current job. This can be done by setting the p query parameter to the value of the related_company custom field using the bricks/posts/query_vars filter.

add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
    if ( $element_id == 'kbmdaf' ) {
        $query_vars['p'] = get_post_meta( get_the_ID(), 'related_company', true );
    }

    return $query_vars;
}, 10, 3 );

where kbmdaf is the Bricks ID of query loop element or the Posts element.

You may want to set a condition on the related company Section to ensure that it gets output only if the field is not empty. For this define a custom function like this:

function bl_get_related_company() {
	$related_company = get_post_meta( get_the_ID(), 'related_company', true );

	return $related_company ? $related_company : false; 
}

and add this Dynamic data condition:

Multiple Select

Objective: On the job CPT’s singular pages, show the related companies and their info incl. the locations.

Same steps essentially as for the Single Select above but with a couple of changes.

Change the filter code to:

add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
	if ( $element_id == 'kbmdaf' ) {
		$query_vars['post__in'] = get_post_meta( get_the_ID(), 'related_companies' );
	}

	return $query_vars;
}, 10, 3 );

You may want to set a condition on the related companies Section to ensure that it gets output only if there is at least 1 related company. For this define a custom function like this:

function get_count_related_companies() {
	$related_companies = get_post_meta( get_the_ID(), 'related_companies' );

	return count( $related_companies );
}

and

References

https://www.php.net/manual/en/function.count.php

https://luetkemj.github.io/wp-query-ref/

Instant access to 390+ Bricks code tutorials with BricksLabs Pro

1 comment

  • Call Me CG

    I really appreciate this tutorial. Thanks for explaining this in a clear way with screenshots -- saved me a few hours!

Leave your comment