Sauter la navigation

[Résolu] How to check if a post has a related post

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
Can we somehow check if a post has a (one) related post, and display or hide content based on whether the post has or has no related post?

Solution:
Yes, you can take 2 approaches:
With Blocks or with ShortCodes.

Further details here:
https://toolset.com/forums/topic/how-to-check-if-a-post-is-in-a-specific-relationship/#post-1606811

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

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Ce sujet contient 4 réponses, a 2 voix.

Dernière mise à jour par alexG-4 Il y a 4 années et 8 mois.

Assisté par: Beda.

Auteur
Publications
#1606471

I have a CPT "Web Properties" and a Post Type "Feed Source" created by WP RSS Aggregator.

I have a 1-1 relationship: 'Web Properties' - 'Feed Sources'

In the context of a specific Web Property, I want to check whether it has a Feed Source related to it.

I can find no documentation nor any support ticket about this.

I tried this:

[wpv-conditional if="( '[wpv-post-slug].item(@web-property-feed-source.child)' ne '' )"]
.......
[/wpv-conditional]

without success.

Do I need to write a shortcode?

Alex

#1606811
Bildschirmfoto 2020-04-30 um 16.26.36.png

Using ShortCodes you could use the Conditional / If button in any text editor to add the condition.
Since you simply want to check if a given post (current) has any related posts, you can check on the "related" Post ID with "if is empty/not empty)
This will produce something like:

[wpv-conditional if="( '[wpv-post-id item="@your-relationship.parent"]' eq '' )"]This post has no related post[/wpv-conditional]

Above would display "This post has no related post" when the post ha no related post, and nothing if there is a related post.
By changing the "eq" to "ne" you can invert the condition and output things when there are related posts, and nothing if not.

Using a Conditional Block instead, you can do the same but you'd never need to edit ShortCodes.
(I've added a screenshot of that setting)

#1606917

Thanks Beda - I've got it working now.

Originally, I used

'[wpv-post-slug].item(@web-property-feed-source.child)'

(see above) which DID NOT work, but

'[wpv-post-slug item="@web-property-feed-source.child"]'

DOES work

I found the [---].item .... syntax somewhere on the forum. It appears that it is not equivalent to the [---- item=....] syntax.

Are there any circumstances where it's correct to use the [---].item.... syntax?

Alex

#1606923

Yes, the first syntax you share is simply not valid: it seems manually constructed.

There is no such syntax as you use in the first example because there the ShortCode closes already before the actual attribute:
It cannot work like that by definition of a ShortCode

The syntax produced by the GUI is the right one to use.

#1606989

My issue is resolved now. Thank you!