Skip Navigation

[Resolved] Relationhip not displaying in Post Source

This support ticket is created 4 years, 7 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 12 replies, has 2 voices.

Last updated by John 4 years, 7 months ago.

Assisted by: Nigel.

Author
Posts
#1723615
2020-07-31_05-43-51.jpg
2020-07-31_05-45-08.jpg

I am trying to:

We have two Posts types Books and Extras. Defined relationship One Book can Have Many Extras. Relationship is active.

If I go to a book or extra I can connect ok.

When I go to the Book Template and try to add a field from the Extra, the Child in Book Extras relationship is not showing

#1723643

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi John

You have a one-to-many relationship between Books and Extras, and from the Book template you want to display some information from related Extras posts.

As there may be many related extras, there isn't a unique source for the field, how should the block know which child extras post to use as the source?

In this case you need to create a View to list the child posts, and then in the output section of that View you will display the desired fields belonging to the child posts.

The other way round you don't need to use a View, because from the context of an "Extra" there can be only one parent, which can be specified as a source directly.

When creating the View you will need to add a Query Filter for the relationship (i.e. show Extras related to the current Book). This workflow will be simplified in the next release, which you can see in the current beta version of Blocks.

#1723689

Sorry Nigel. I knew that. Must have taken the stupid pill this morning.

One related question. A book may or may not have extras. We want to display a heading block “Extras” and the view block only when there is at least one Extra linked to the Book. How do we test for that in the conditional block they would be wrapped in?

#1723705

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

This is one of those things that is a little tricky in Blocks compared to classic Views.

To avoid duplicating querying the database for your View results, you could previously add the heading to the output of the View, inside the results found section but before the loop itself (so it would only be output once, and only if there were results).

That's not possible with Views made using the block editor, so you need to use the output of the View itself in the conditional test (checking whether it equals the "No items found" text, or something else), and for complex Views or where you are not using pagination that can add quite a bit more overhead to your queries.

In this case I would be inclined to define a custom shortcode or function that uses the relationships API and returns the number of child posts for the current post, and use that in the condition.

Here's an example of a general-purpose shortcode to do just that which you can use: hidden link

You will need to register the custom shortcode at Toolset > Settings > Front-end content before it can be used for your condition.

So add a conditional block, using that shortcode for the condition, and include both the heading block and the View itself inside this conditional block.

If you get stuck, let me know.

#1723709

Thanks! Let us give it a try.

#1723717

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I just created an internal request ticket to be able to do the same in Blocks as was previously possible with classic Views (add content to the items-found section outside of the loop). I recall during the design phase we were trying to simplify the UI when creating a View, but may have gone a little too far.

#1723731

Thanks. Use case is pretty common for us. Need to do the same thing with a many to many between books and blog posts (same for authost and series)

That way content managers can do post, link one or more books, authors and/or series to it. The iview of linked object shows below the post. On the book page, same will let us show posts linke dto that book

#1723847

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I expect it will be a while before there are any changes to the plugin, although hopefully we'll find a way to facilitate it within the UI without complicating things.

Did you try the code workaround?

#1723849

Thanks! Working on it now. Will let you know if we have questions.

#1726569

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

How'd you get on?

#1728829

Got it working for Books to Extras (The Extras section only shows now when they are linked!)

Working on Posts tp Books (These are a many to many

#1729023

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

OK, that same custom shortcode should work with many-to-many relationships, too.

#1732283

My issue is resolved now. Thank you!