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 610 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
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
Related Episodes Grouped by Episode Chapters on Single Podcasts in Bricks when using JetEngine

Related Episodes Grouped by Episode Chapters on Single Podcasts in Bricks when using JetEngine

A user asks: How to Display Related Episodes Grouped by Chapters on a Podcast Page in Bricks? Hi everyone, I have two post types created…
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
ACF Relationship: Show all other events related to the artists of the current event

ACF Relationship: Show all other events related to the artists of the current event

How all other events related to the artists of the current event can be output in a Bricks query loop.
Categories:
Pro
Pods in Bricks

Pods in Bricks

Updated on 3 Aug 2023 This Pro tutorial is going to be a collection of tips and code snippets for using/implementing Pods in Bricks builder.…
Categories:
Tags:
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.
Pro
ACF Relationship Select Filter in Bricks

ACF Relationship Select Filter in Bricks

In the BricksLabs Facebook group, a user asks: I'm learning about ACF relationships and attempting to output a list of posts on any given page,…
Categories: