Skip Navigation

[Résolu] Using a post id shortcode to determine child post display data

This support ticket is created Il y a 5 années et 6 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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+01:00)

This topic contains 3 réponses, has 2 voix.

Last updated by tony Il y a 5 années et 6 mois.

Assisted by: Nigel.

Auteur
Publications
#1121382

I am trying to do something that seems simple but my coding skills are not quite up to the task, having spent several hours searching for a solution!

I have a PLACE post that displays ORGANISATION posts in a list.

Some of the ORGANISATIONS are children of the PLACE and others are not.

My list of organisations includes the organisation name and the parent place but I want to suppress the display of the organisation name where the organisation is a child of the PLACE.

I tried to do this with a function that stored the parent PLACE in a field by using this shortcode function:

=====================================
add_shortcode( 'return_post_id', 'the_return_post_id' );

function the_return_post_id() {
return get_the_ID();
}
======================================

so that I could use a conditional test by comparing the post_id of the place with the post_id of the place parent in the organisation post.

Unfortunately the post_id of the place gets overwritten by the post_id of the organisation so my conditional cannot work.

See hidden link

The place post_id is 193.

The first 3 organisations have a parent place post_id of 193 and I want to stop Mickleton being displayed. The last organisation has a parent place post_id of 267 and so I want to display the parent name Grassholme Reservoir.

Please can you help refine my code as I am sure this would be useful to others?

Many thanks
Tony

PS Is there some code that can be added to the sortcode to only store the post_id if the post type is a PLACE?

#1121907

get_the_ID(); gets the post that is now in the loop.
https://developer.wordpress.org/reference/functions/get_the_id/
You will not receive a parent post or any related data with that Code.

The Toolset has a lot of ShortCodes that mostly cover all cases (so the one coded above, as it will return the Post ID)
Here is a list of things you can already access with Toolset as shortcodes:
https://toolset.com/documentation/user-guides/toolset-shortcodes-menu/
https://toolset.com/documentation/user-guides/views-shortcodes/
https://toolset.com/documentation/toolset-training-course/toolset-shortcodes/

However, to compare if a post has a Child is not something is natively possible, in this direct sense.
Previously with the old kind of relationships you could do some tricks as in checking if the field which connected posts isn't empty, or of a certain value.
Today, Types stores the relationships in custom database tables and you'll need some API Custom Code to get that data if you use ShortCodes manually crafted.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

You would, depending on the type of relation, get the related post(s) and for example, return true/false in a Custom ShortCode if you found items, or not.
That could then help to craft a conditional as you plan.

It depends on a lot whether this is a one to many relationships or a many to many relationships.
In the first case you would not even need code, but could just use this condition in a View that displays Posts of Child Type:

[wpv-conditional if="( '[wpv-post-id item='@page-post.parent']' eq '[wpv-post-id item='$current_page']' )"]This is only if the current post is child to the post where the view is shown [wpv-post-title]<br>[/wpv-conditional]
          [wpv-conditional if="( '[wpv-post-id item='@page-post.parent']' ne '[wpv-post-id item='$current_page']' )"]This is only if the current post is not child to the post where the view is shown<br>[/wpv-conditional]

I made a module while testing the above, it's here.
hidden link

It holds a View and a Relationship between pages/posts in the one to many ways.
Create a page, few child posts to it, and a few posts that are not a child to it.
Insert the view to that page and view it.
(Do this on a staging site)

Let me know if it helps.

#1122016

Thanks Beda

You told me what I needed to know and that was the code to get the post_id of the page/post being displayed.

[wpv-post-id item='$current_page']

I am not sure where I should have found that in the help pages.

I just adjusted the conditional to:

  [wpv-conditional if="( '[wpv-post-id item='@place-organisation-venue.parent']' eq '[wpv-post-id item='$current_page']' )"] <br />[/wpv-conditional]
  [wpv-conditional if="( '[wpv-post-id item='@place-organisation-venue.parent']' ne '[wpv-post-id item='$current_page']' )"][wpv-post-link item="@place-organisation-venue.parent"]<br />[/wpv-conditional]

It now displays exactly what I wanted.

Great thanks.
Tony

#1122017

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.