4th Nov '22
/
1 comment

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 624 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 Bricks Query Loop By Meta Box Relationship

Filtering Bricks Query Loop By Meta Box Relationship

In the Bricks Facebook group a user asks: Consider this setup: CPTs: Service Areas and City Pages Meta Box Relationship: City Pages to Service Areas…
Categories:
Pro
Bidirectional Relationship between a CPT and a Taxonomy of another CPT using ACF in Bricks

Bidirectional Relationship between a CPT and a Taxonomy of another CPT using ACF in Bricks

A couple of members asked: I have a cpt called "Markets" aand a cpt "tools". Tools have a taxonomy "tools group". How can i create…
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…
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
Bricks Forms + Meta Box: Auto-Create Post Relationships on Form Submission

Bricks Forms + Meta Box: Auto-Create Post Relationships on Form Submission

How to link a Bricks post-creation form on a post to a newly created post of another post type using Meta Box relationship.