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 lien caché
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?
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.
lien caché
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.
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
My issue is resolved now. Thank you!