Skip Navigation

[Gelöst] Conditional output to check for related posts

This support ticket is created vor 4 Jahre, 6 Monate. 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.

This topic contains 2 Antworten, has 2 Stimmen.

Last updated by zoeS vor 4 Jahre, 6 Monate.

Author
Artikel
#905470

Thanks all of you for the latest release with updated relationships, which is fantastic. I expect you'll be pretty busy right now with support requests, but just one thing – previously I had a custom shortcode that could be used to check for the existence of a child post before outputting code in my view. With the new relationships is there a simpler way of doing this, where there is no intermediary post type?

So basically I'm creating the content template for post type A, and I have a section where it displays which posts of type B it is connected to, but only if it has any such connections.

#905539

Any code that you created related to "simple" queries like this one should proceed to work on relationships that were created before the new updates.

If you were to create new relationships, then I suggest using the new API for them.
This is elaborated here:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

You can use (for example) the function toolset_get_related_post(), and if the returned result is "zero", you know there are no related posts.
That could as well be returned in a ShortCode, and used in conditionals, or used directly as a conditional source in an HTML condition (if you register the function in Toolset > Settings)

But actually, if you just create a View that displays the connected posts and insert it into your Content Template, that View already takes care of the "nothing found" case, in the Loop.
Might it be much easier to just style the "No items found" section of the view?

#905753

Thanks so much Beda, you're right, it was easiest just to take all of the surrounding code in to the view and leave the "no items found" section blank.