Pods Relationship Field in Bricks

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/

Get access to all 524 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

 

Related Tutorials..

Pro
Filtering ACF Relationship Query by Post Meta in Bricks

Filtering ACF Relationship Query by Post Meta in Bricks

How to filter the posts of a post type related to another post type based on the value of a True / False type ACF…
Pro
Meta Box Relationship in Bricks using Posts Query Loop

Meta Box Relationship in Bricks using Posts Query Loop

This Pro tutorial is similar to the recent ACF Relationship in Bricks using Posts Query Loop guide but for Meta Box. In the past, we…
Categories:
Pro
Sorting ACF Relationship Posts by Date Meta in Bricks

Sorting ACF Relationship Posts by Date Meta in Bricks

Consider the following setup: CPT: Events ACF Fields:Event Date Information (Group)|__Event Date (Date Picker)Pick Sub Events (Relationship) An Event can have 1 or more related…