Skip Navigation

[Resolved] Display a parent child related posts in a tree-like structure

This support ticket is created 2 years, 3 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 22 replies, has 2 voices.

Last updated by Franco Calcagni 2 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#2440625

We can handle the questions one by one
1) hidden link

It is a "Scheda" post, it isn't "ARCHITECTS" post

The relationship "scheda_scheda-studio-arch":
hidden link
It is one-to-many relationship between post type "Schede" and "Schede Studi Architetti"

There are 21 post type relationships in your website, to avoid more misunderstandings, please confirm those three questions first.

1) Are we talking about four custom post types?
- Schede
- Studi
- Architetti
- Schede Studi Architetti

2) Are we talking about those three post type relationships?
- one-to-many relationship between "Schede" and "Schede Studi Architetti":
hidden link
- one-to-many relationship between "Studi" and "Schede Studi Architetti":
hidden link
- one-to-many relationship between "Architetti" and "Schede Studi Architetti":
hidden link

3) You are going to:
In single "Scheda" post, display related "Architetti" posts in tree like struture.
STUDIO 01
- Architect 01
- Architect 02
- Architect 03

STUDIO 02
- Architect 02
- Architect 03

#2440675

Hi Luo,
I reply the following points but first I have to tell you that once we migrated to the new version of post relationships of Toolset (the structure was initially created with the old version of Toolset), I discovered that now in the Scheda post editing screen we cannot see the box named “relationships” that allowed us to created directly in the SCHEDA post the relationships between SCHEDA / STUDIO / ARCHITECT.
Apart from this, the post types involved are
SCHEDE (every scheda can be related to many studios and many architects)
STUDI (every studio can be related to many schedas and many architects)
ARCHITETTI (every architect can be related to many studios and many schedas)
So basically the relationship are all many to many.
In all above post types we have to display only the other related elements to the specific post (I.e. in the SCHEDA are displayed ONLY the STUDIO and ARCHITECTS only related to that post, in the STUDIO post type are displayed ONLY the architects and studios related to they studio, in the ARCHITECT post type we shall display only the schedas and the studios related to that architect.
2) the post relationships you listed are correct but as I wrote above when we did the migration to the new Toolset relationships we lost the possibility to create the SCHEDA / STUDIO / ARCHITECT relationship directly in the SCHEDA post
3) the structure you listed is OK (please consider that the data displayed shall be OKLY those related to that specific post.

I look forward to your reply

Thanks

Franco

#2442321

Thanks for the confirmation.

Since in those three post type relationships:

- one-to-many relationship between "Schede" and "Schede Studi Architetti":
- one-to-many relationship between "Studi" and "Schede Studi Architetti":
- one-to-many relationship between "Architetti" and "Schede Studi Architetti":

There isn't direct relationship between "Schede" and "Studi", "Studi" and "Architetti".

So you can not display the the results as "tree like struture"

I have setup a demo in your website:
hidden link

			<li>
              	[wpv-post-link item="@studio_scheda-studio-arch.parent"]
              		<ul>
                      <li>[wpv-post-link item="@architetto_scheda-studio-arch.parent"]</li>
                    </ul>
			</li>

See the result here:
hidden link

It will display duplicated item "studio" items:
Park Associati
- Michele Rossi
Park Associati
- Filippo Pagliani

#2442379

Hi Luo,
thanks for the update, the problem is that in your above solution the "Park Associati" element is displayed every time for every architect that appears, what we need to achieve is to see the STUDIO as a parent element and the ARCHITECTS as child of that element like in the following structure:

Park Associati
- Michele Rossi
- Filippo Pagliani

Please let me know if this solution can be achieved.

Thanks

Franco

#2443049

As I mentioned above:

There isn't direct relationship between "Schede" and "Studi", "Studi" and "Architetti".

So you can not display the tree-like structure within Toolset plugins, but it is possible with custom codes, I have setup a demo in your website:
1) Add a Custom code snippet "tree-like structure", with below codes:
hidden link

add_shortcode('tree-like-structure', function($atts, $content){
  $scheda_id = get_the_ID();
  $intermedia_posts = toolset_get_related_posts($scheda_id, 'scheda_scheda-studio-arch', [
        // Specify the role to query by and to return. 
        // Notice that we can use "other" in this situation.
        'query_by_role' => 'parent',
        'role_to_return' => 'child',]
  );
  $studios = [];
  foreach($intermedia_posts as $k=> $v){
  	$studio_id = toolset_get_related_post( $v, 'studio_scheda-studio-arch' );
  	$architetto_id = toolset_get_related_post( $v, 'architetto_scheda-studio-arch' );
    if($studio_id){
      if(!isset($studios[$studio_id])){
     	 $studios[$studio_id] = '';
      }
      if($architetto_id){
        $studios[$studio_id] .= sprintf(
        	'<li><a href="%1$s">%2$s</a></li>', 
        	get_permalink($architetto_id), 
            get_the_title($architetto_id)
        );
      }
    }
  }
  $res = '';
  foreach($studios as $studio_id => $architettos){
    $res .= sprintf(
        	'<li><a href="%1$s">%2$s</a> <ul>%3$s</ul></li>', 
        	get_permalink($studio_id), 
            get_the_title($studio_id),
      		$architettos
    );
  }
  if($res){
     $res = '<ul>' . $res . '</ul>';
  }
  return $res;
});

2) Edit the content template
hidden link
Replace the view block with a shortcode block, and display above custom shortcode: [tree-like-structure]

Test it in frontend:
hidden link

It works fine, for your reference.

More help:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

#2443199
relationships.jpg

Hi Luo,
thanks for the new update that now works fine.
The problem is now for the editing part of the new SCHEDA where in the OLD site we had a box that allowed us to work with the relationship between SCHEDA / STUDIO / ARCHITECT, but after the migration to the new Toolset version of the relationships we lost the editing possibility for creating the relationship you say is missing.
I enclose herewith a screenshot of the relationships box that is active on the old site and how it looks like on the new site.
Actually, we need to create the possibility in the SCHEDA to create directly the relationship between the STUDIOS and the ARCHITECTS as we did in the past. Once we populated that box in the old site, a relationship between that STUDIO and ARCHITECTS was created.
Then we need to show in the frontend not only the post title with a link but also a thumbnail of the featured image as you see in the following post of the old site:

hidden link

Please have a look at the attached screenshot and let us know if with the new relationship system of Toolset we can build (even from scratch) a relationship that can connect SCHEDA / STUDIO / ARCHITECT

Thanks,

Franco

#2443229

I assume the original questions of this thread is resolved:
https://toolset.com/forums/topic/display-a-parent-child-related-posts-in-a-tree-like-structure/#post-2434399

According to our support policy, we prefer one ticket one question, for the other issues, please check the new thread here:
https://toolset.com/forums/topic/new-toolset-version-of-the-relationships-we-lost-the-editing-possibility-for-creating-the-relationship/

#2443233

Hi Luo,
the original question is solved.
I am going to open a new ticket for the other things connected with it.

Franco