Skip Navigation

[Resolved] Conditional to test if post is connected via relationship or similar effect

This support ticket is created 5 years, 2 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.

Our next available supporter will start replying to tickets in about 5.75 hours from now. Thank you for your understanding.

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)

This topic contains 5 replies, has 2 voices.

Last updated by Ben 5 years, 2 months ago.

Assisted by: Beda.

Author
Posts
#1204733

Ben

I would like to be able to use conditionals in a View to display whether a post has been connected to another post via a relationship. Or output a predetermined message if not, or have some sort of solution that gives a similar effect.

For example, I would like to see if CPT Books is connected with CPT Author. I could use [wpv-post-title item="@RELATIONSHIPSLUG.parent"] to display the title of the connected post if it exists, but if it doesn't nothing is displayed. Can I set something to be displayed here instead of it being blank? Or is there another way of achieving these desired results?

#1204953

Sure, you can use HTML conditional on the field that you mention (for example, post title).
Note this works only for One To Many or fewer relationships, not for Many To Many, as there we never know how many, and what posts, are connected (there can be many)

So you would have a condition like:

[wpv-conditional if="( '[wpv-post-title item="@posttypeone-posttypetwo.parent"]' ne '' )"]
  Anything if the post title in parent exists
[/wpv-conditional]

It can be generated with the GUI for conditional output.

Note, there is a little issue here.
When you generate the condition with the GUI, you will get this:

[wpv-conditional if="( '[wpv-post-title item="@posttypeone-posttypetwo.parent"]' ne '' )"][/wpv-conditional]

That seems wrong because reddish highlighted - however, it is not wrong, you should always alternate double and single quotes. Hence, this is correct and you can ignore the reddish highlighting.

#1205013

Ben

Thank you for your reply Beda.

Unfortunately the relationship that I am working with here is a many to many relationship so this will not work. I should of mentioned this before, sorry!

Is there any alternative or any way to make a similar sort of solution work with a many to many relationship?

#1206211

No, unfortunately in a Many To Many Relationship you need a View to display or check it's related posts as there might be many related posts.

So, if you are on Post A of Post Type A and that post is related to Post Type B (many posts), this is like checking on a parent post if it has any child posts, something that is and was not possible in Toolset.

In this case, custom code is required, or you can try to play with Views and the wpv-items-found shortcode.
Let's say, you produce a view, and make sure to output raw values (check the box "Disable the wrapping DIV around the View" under the loop editor), and put within the View loop (inside the wpv-loop) the wpv-found-count or even just the number "1".
Then, limit this view to return only ONE result and query the post type related to the post you want to check if it has related posts.
Set as well a query that says "show posts related to the post where this view is inserted to"

Now, in a wpv-conditional, you should actually be able to check if that View's result is 1, or any else.
If it is any else, there are no related posts to the current post.
If the result is 1, there is at least one related post.

Please let me know if it is clear what I mean, here are the steps:

1. Create a post-relationship (m2m - many to many)
2. Connect many posts to many posts
3. Create a View to query the one end of the posts (let's say post type A)
4. Set a Query filter that "Select posts in a {your-relationship} relationship that are related to the Post where this View is shown."
5. Limit the View to one output only.
6. Insert [wpv-found-count] or better, just the number "1" in the Loop Editor and check "Disable the wrapping DIV around the View"
7. Now check (in a related single post) if the View returns 1 or anything else:

[wpv-conditional if="( '[wpv-view name="another-cview"]' eq '1' )"]yes has posts[/wpv-conditional]

But, note, I have issues with step #7 above, which I would like confirmed by you first.
What is expected is that condition either shows "yes has posts" if there is a connected post and obviously, nothing if there is no connected post.
Now, the View itself does work, if you output it, you will see "1" if there is a connected post.
But, if you analyse the HTML code you will see there is a lot of space around that 1, and that breaks the condition.
Is this the case as well on your install?
I will then escalate this with some urgency as it would be a bug.
If not, then it is just my install that is somehow corrupt (I tried a fresh install, but it's still the same 🙁 )

Please let me know if the solution works for you or not.

#1206219

Related to #7, I was wrong.
It works but you need to follow another process.

When you create the View's loop choose the "Loop WIzard" and then "List with separators"
Insert the loop and remove the ShortCode [wpv-post-title] it generated, replace it with "1".
It should now look like this:

<wpv-loop>
			[wpv-item index=other]
				1,
			[wpv-item index=last]
				1
		</wpv-loop>

That will now work fine in the conditional 😉

#1209381

Ben

Thank you so much for you clear and concise help with this difficult issue Beda.

This enables me to do exactly what I need to do, thank you so much!

Thank you for all your help with this!

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