Skip Navigation

[Resolved] Checking value returned by wpv-relationship-filter using Conditional Block

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

This topic contains 15 replies, has 3 voices.

Last updated by Nigel 11 months, 3 weeks ago.

Assigned support staff: Nigel.

Author
Posts
#2247143

Hi,

I have added a view on a page and I would like to display the view only if a value has been passed to wpv-relationship-filter parameter (for e.g., ?wpv-relationship-filter=40), else a custom message will be displayed.

However, in the Conditional Block > Views shortcode, there is no such option. I tried Archive title and Search term, but none of them worked.

I also referred to the documentation but can't find any working examples.

The problem here is that if no value is passed to wpv-relationship-filter parameter, the view displays all the posts.

Is there anyway I can check if a value has been passed to wpv-relationship-filter parameter.

#2247217

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Alok,

Thank you for getting in touch.

You should be able to achieve this by using the [wpv-search-term] shortcode.

To use this you will just provide the param parameter with the URL parameter that you want to get the value for and from there you can simple check if that value is empty with our conditional shortcodes.

Example
[wpv-search-term param='wpv-relationship-filter']

For more information on the shortcode you can check the link below.

https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-search-term

Thanks,
Shane

#2247483

Hi Shane,

( empty( '[wpv-search-term param="wpv-relationship-filter"]') does work, but only if the URL contains ?wpv-relationship-filter=xx parameter.

If the page is accessed directly, without ?wpv-relationship-filter=xx parameter, then the view shows all the results.

I believe I need to put two conditions, the 1st condition will check if the URL contains ?wpv-relationship-filter=xx parameter. If yes, then it will check if any value is returned by ?wpv-relationship-filter=xx parameter.

Alternatively, is there any way to prevent the view from displaying all the results if it cannot find any value returned by ?wpv-relationship-filter=xx parameter?

Can you please help me out with this?

Thanks.

#2247551

Hi Shane,

I figured out the problem.

I have added two conditional blocks -

1st Conditional Block - if condition returns empty, then display "Nothing found";

2nd Conditional Block - if condition returns not empty, then display the View.

But irrespective of the result, the 2nd condition is still executing, and I can't figure out why.

#2248861

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

Shane is on vacation this week, so let me step in.

I tried this out on my own test site and found the most reliable test was simply to check whether the search-term for the specified parameter was equal to an empty string or not.

So I have two conditional blocks, the first of which is intended to show when the parameter is missing, so the condition looks like:

  ( ( '[wpv-search-term param='wpv-relationship-filter']' eq '' ) )

(Because the wpv-search-term shortcode needs an attribute to specify the URL parameter the condition must be entered in the Advanced Editor.)

The second block is to show something when the parameter is present in the URL, and looks like:

  ( ( '[wpv-search-term param='wpv-relationship-filter']' ne '' ) ) 
#2248939

Hi Nigel,

That is not the problem.

The issue is that the Views block gets executed even before the Conditional block can execute and displays the results. You can check it for yourself by visiting the following links:

Without the parameter, the View block should not be executed, but it still gets executed - hidden link

With the parameter, the View block will obviously get executed - hidden link

However, I didn't notice this with other blocks.

So it might be some kind of bug, either with the conditional block or views block.

#2249007

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I think it must be how you've added the conditional block, because that's not how it works and I just checked if there was something particular about the View block that was different, but it doesn't get rendered if it is inside a conditional block where the condition fails.

I can take a look at how you've added this if I can get access to your site.

Let me mark your next reply as private so that we can get log-in credentials from you—you may want to create a temporary admin user for us to use that you can later delete. And be sure to have a current backup of your site.

#2249601

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I tried to access the site with those credentials but they don't work and I'm running out of attempts.

Could you please edit your last reply and update with new credentials?

Thanks.

#2249669

Hi Nigel,

I have updated the password and also tested it at my end.

Can you please try again?

#2249913

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

It seems like your hCaptcha plugin doesn't work with Firefox, I couldn't access your site with the updated credentials, but when a colleague could I tried again in a different browser and was able to.

I struggled to understand what is going on in your site, I couldn't account for what I was seeing on the page in question, with texts repeated unexpectedly in various places on the page, which leads me to suspect they are being injected via some filter I am unfamiliar with.

So I took a copy of your site so that I could disable all unnecessary plugins and switch theme, and the odd behaviour on that page persisted, but wasn't limited to that page, even the normal plugins page in the back end couldn't be accessed, I don't know what was going on.

So I simply created a new page, inserted two conditional blocks with the conditions I described in https://toolset.com/forums/topic/checking-value-returned-by-wpv-relationship-filter-using-conditional-block/#post-2248861, one to show content when the url parameter was set, and another to show content when it was not.

I added paragraph blocks to identify the output, and added a View block (using your existing View) to the conditional block for when the url parameter is set, and then tested the results on the front end. They worked as expected, with the View results appearing only when the url parameter was set.

I went back to your site to do the same, add a test page with the same set up, but new pages are not set to use the Gutenberg editor so stopped there.

I can't account for the strange behaviour of your site, but suggest you add a new page as described, with the two conditional blocks having the same conditions as described above so that you can verify the conditions work correctly.

#2250613

It seems like your hCaptcha plugin doesn't work with Firefox, I couldn't access your site with the updated credentials, but when a colleague could I tried again in a different browser and was able to.
It is difficult to say why hCaptcha was not working correctly at your end, but at my end, it is working correctly in all the browsers. If you can let me know what exactly happened, I will try to find out. Also, the login pages are protected using Cloudflare.

I struggled to understand what is going on in your site, I couldn't account for what I was seeing on the page in question, with texts repeated unexpectedly in various places on the page, which leads me to suspect they are being injected via some filter I am unfamiliar with.

So I took a copy of your site so that I could disable all unnecessary plugins and switch theme, and the odd behaviour on that page persisted, but wasn't limited to that page, even the normal plugins page in the back end couldn't be accessed, I don't know what was going on.

So I simply created a new page, inserted two conditional blocks with the conditions I described in https://toolset.com/forums/topic/checking-value-returned-by-wpv-relationship-filter-using-conditional-block/#post-2248861, one to show content when the url parameter was set, and another to show content when it was not.

I added paragraph blocks to identify the output, and added a View block (using your existing View) to the conditional block for when the url parameter is set, and then tested the results on the front end. They worked as expected, with the View results appearing only when the url parameter was set.
I am still clueless as to why this issue is not happening at your end and why it is happening at my end.

I went back to your site to do the same, add a test page with the same set up, but new pages are not set to use the Gutenberg editor so stopped there.
I am using Beaver Builder and hence have switched to Classic editor as default editor. But I have changed it to Blocks editor.

I can't account for the strange behaviour of your site, but suggest you add a new page as described, with the two conditional blocks having the same conditions as described above so that you can verify the conditions work correctly.
I have created a Test Page (page ID: 507) with the same contents and encountered the same issue. I then removed the Views block and replaced it with a paragraph, and surprisingly it is working as desired. But as soon as I add the Views block, the same thing. So for now, I have removed the Views block so that you can check it out for yourself.

Without parameter - hidden link

With parameter - hidden link

Like I mentioned earlier, the views block is executing irrespective of the condition, i.e., true or false.

As for the hosting setup, just to let you know that the issues I am facing are specifically related to Toolset only. All other plugins are working fine. I have reported issues related to Toolset behaving strange somewhere in the previous month.

Just for the sake of Toolset, I have changed three hosts, from Digital Ocean to Vultr to Linode. I even tried a clean installation on Cloudways free trial and encountered the same issue there also.

So it cannot be said that the problem would be related to the hosting infrastructure. There is definitely something wrong. This is just one another issue that I am facing with Toolset, there are dozens that I haven't reported yet.

Anyway, you please have a look at the Test Page to see it for yourself. After that, you may add the views block and check again.

#2250741

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

It looks like it is a problem specific to that View.

I edited your test page and added a new View (called Nigel) to the second Conditional block, and the conditions continue to work as expected on the front end.

I can't see what is wrong with the existing View you have been using, but there appears to be something under the hood that is particular to that View which creates the issue.

I'm afraid you'll need to delete it and replace it with a new View.

#2250783

Hi Nigel,

I had a hard time figuring out what is causing the view to display irrespective of the condition, but finally figured it out.

I did as you suggested. I deleted the existing view and created a new view. The clean view (without any blocks) worked perfectly fine, but as I started adding blocks, the problem came back. I then deleted one block and finally found the culprit.

The culprit here is the Post Excerpt field which is causing the conditional block to break, irrespective of the condition, especially if the wpv-relationship-filter parameter is not specified.

hidden link
hidden link

If you check the page again, it is working perfectly fine. But as soon as you add the Post Excerpt field, the conditional block will again break.

You may want to escalate this issue to the developers for further investigation. Without the Post Excerpt field, the view is meaningless.

#2250803

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Alok

Well spotted.

We've had one or two issues inserting post excerpts with blocks before, so I tried the known remedy of replacing it with a Fields and Text block in HTML mode, and adding the excerpt using the wpv-post-excert shortcode, and that appears to be working correctly without any side effects, if you could confirm.

Meanwhile I'll see if I can reproduce the problem on a test site and then report that to the developers, but if using the shortcode works then we can go ahead and close here.

#2250813

Hi Nigel,

Awesome, it is working like a charm.

For now, this will solve the problem and I will go ahead and add views for other CPT relationships.

Please suggest if the thread should be closed, or you would like to check this out and report to the developers.