Skip Navigation

[Resolved] Post Connection not working

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to display a custom field from a child post in the parent post template, but the Types field shortcode is not working as expected.

Solution: You can't use a Types field shortcode in this case, because there can be many child posts associated with the parent post. You must use a View of related posts, filtered somehow, to display the field from the correct child post.

This support ticket is created 5 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 11 replies, has 2 voices.

Last updated by Sammut 5 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1193889

Hi,

I am currently using the following connection and it is working fine.

[wpv-conditional if="( '[connections relationship='issue-entrepreneur-in-elevator']' eq '1' )"]
[column type="1/2" class="video"][types field='featured-video' item='@issue-entrepreneur-in-elevator.parent'][/types]<br /><em>[wpv-post-body view_template="None" item='@issue-entrepreneur-in-elevator.parent']</em>[/column]
[/wpv-conditional]

I created a new connection for a difernt post type and it is not showing.

[wpv-conditional if="( '[connections relationship='batch-video-2']' eq '1' )"]
[column type="1/2" class="video"][types field='featured-video' item='@batch-video-2.parent'][/types]<br /><em>[wpv-post-body view_template="None" item='@batch-video-2.parent']</em>[/column]
[/wpv-conditional]  

In the admin area the connection shows correctly, but now front-end display. can you assist? Thank you!!!

#1193903

Hi, I'm not familiar with the connections shortcode so I'm not really sure how it works. To use a 3rd-party shortcode inside a conditional, you should register it first in Toolset > Settings > Front-end Content. Register "connections" in "Third-party shortcode arguments" as well as in "Functions inside conditional evaluation".

Then, you can figure out why the conditional isn't working as expected by turning on debug mode and simplifying the contents. Try this as an example:

Before the conditional, batch video 2: [connections relationship='batch-video-2']<br />
[wpv-conditional if="( '[connections relationship='batch-video-2']' eq '1' )" debug="true"]
Inside the conditional, Batch video 2 connection eq 1
[/wpv-conditional]

Let me know what you see on the screen with this test code, and we can go from there.

#1193911

It is registered. OK, perhaps there is another approach, how does one test for the existence of a connection? As a side note, even I just use the following it does not work:

[types field='featured-video' item='@batch-video-2.parent'][/types]<br />[wpv-post-body view_template="None" item='@batch-video-2.parent']

Per your debug:

####################
wpv-conditional attributes
####################
Array
(
    [if] => ( '1' = '1' )
    [debug] => true
)

####################
Debug information
####################
--------------------
Original expression: ( '1' = '1' )
--------------------
After replacing 1 general variables and comparing strings: ( 1 = 1 )
	Comparing 1 to 1

The condition shows true and the text inside displays. It seems the issue is with:

[types field='featured-video' item='@batch-video-2.parent'][/types]<br />[wpv-post-body view_template="None" item='@batch-video-2.parent']

Thank you!

#1193924

I'm still not clear - are you talking about Toolset post relationships, or are you talking about connections from a 3rd-party system? I saw you were using the "connections" shortcode before. Toolset doesn't use the "connections" shortcode, and I'm not sure how to access related posts using a 3rd-party post relationships system. If your post relationships were not created in Toolset, the @ syntax used in your code will not work and we'll need to find another approach.

Assuming your relationships were created in Toolset, we can figure out why the shortcodes are not working. Hard-code the parent ID instead of using the @ syntax, and throw in something simple like the post title as well just for confirmation:

[types field='featured-video' item='12345'][/types]<br />
Title: [wpv-post-title item='12345']<br />
[wpv-post-body view_template="None" item='12345']

Replace 12345 with the numeric post ID of the known parent post, and check the results.

To test if a related post exists (Toolset post relationships here, not 3rd-party relationships), there are a couple of options. One is with PHP code using the Toolset Post Relationships API. I can provide some code examples if that's what you'd like to do. The other option does not require custom code. Basically you can use a View of related posts, limited to 1 result. If any items are found, you can display something in the wpv-items-found section. If no items are found, you can display something else in the wpv-no-items-found section. It's like using a View as a conditional. I can provide more information about that if you'd like.

#1193925

My apologies. I am talking about showing parent content in Toolset. If you have a look here: hidden link you will see two videos. Those videos are displaying using the following:

[types field='featured-video' item='@issue-video.parent'][/types]

AND

[types field='featured-video' item='@issue-entrepreneur-in-elevator.parent'][/types]<br /><em>[wpv-post-body view_template="None" item='@issue-entrepreneur-in-elevator.parent']</em>

We added more relationships to the page and they are not working, E.g.,:

[types field='featured-video' item='@batch-video-4.parent'][/types]<br /><em>[wpv-post-body view_template="None" item='@batch-video-4.parent']</em>

We did everything the same yet these new post type relationships are not showing on the front end despite they are showing up int he admin as being a post type relationship.

#1193926

Using the snippet you sent over with the parent id, it pulls in the main content for that post id but not the related content.

Batch-Video-X is in a one to many relationship. Meaning one issue can have many videos associated with it. This is working correctly all over the website except for this one custom post type.

#1193930

May I take a look in wp-admin to see how this is set up? Private reply fields are active here.

#1193937

Okay I'm looking at the Batch post here:
hidden link

The Content Template used to display this post is here:
hidden link

In the Content Template, you're using the post relationship syntax to access a parent post for the relationship "batch-video-2"...which is the current Batch post? If the custom field is stored on the Batch post, there's no need for this @ syntax. If the custom field is stored on another post, then which post is it?

#1193942

For each batch post there can be up to 5 video slots:

batch-video-1
batch-video-2
batch-video-3
batch-video-4
batch-video-5

Each of the above is a one-to-many relationship where each batch can have many videos (custom post type).

So for batch one, we selected the specific video to display. The others are working, that is what is odd!

#1193957

It appears in order to do what I want it needs to be a many-to-many relationship.

#1193963

The others work because:
Issue Video is a parent of Batches
Entrepreneurs in Elevators is a parent of Batches
So the video field is in the parent of the current Batch post.
In both of these cases Batch is the child. There can be only one parent Issue Video and only one Entrpreneurs in Elevators post for each child Batch.

However,
Batches are parents of Videos in the Batch-Video-2 relationship. Video is the child, so this type of syntax will not work. Where is the Video? It is in one of the child Video posts, right? Since there can be multiple child Videos for the current Batch parent, you need to know which Video to display. So which is it? How is it determined?

#1193979

My issue is resolved now. Thank you!