Skip Navigation

[Resolved] Compare text of 2 fields in wpv-if statement

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 0.26 hours from now. Thank you for your understanding.

This topic contains 7 replies, has 2 voices.

Last updated by Becky 6 years, 3 months ago.

Assigned support staff: Beda.

Author
Posts
#298435

I need to compare 2 text fields in a content template. If they're the same, show abc. If they're different, then show xyz.

The following code works in the sense that it outputs, but it's outputting either one or the other, but not both.

[wpv-if r1="[wpv-post-field name="field_1"]" r2="[wpv-post-field name="field_2"]" evaluate="$r1 = $r2"]ABC[/wpv-if]

and

[wpv-if r1="[wpv-post-field name="field_1"]" r2="[wpv-post-field name="field_2"]" evaluate="$r1 != $r2"]XYZ[/wpv-if]

I've also tried using the attribute "condition="true|false" and that doesn't seem to work either.

Where have I gone wrong?

Thanks,
Becky

#298631

Thank you for contacting us here in the Support Forum

Please see here the correct code to evaluate Custom Post Fields one against the other:

To check if the value of the Custom Filed 1 is the same as the value of Custom Field 2, and output XYZ if it is:

[wpv-if f1="wpcf-text-1" f2="wpcf-text-2" evaluate="$f1 = $f2" debug="true"]XYZ[/wpv-if]

To check if the value of the Custom Filed 1 is NOT the same as the value of Custom Field 2, and output ABC if it is not the same:

[wpv-if f1="wpcf-text-1" f2="wpcf-text-2" evaluate="$f1 != $f2" debug="true"]ABC[/wpv-if]

If you evaluate certain values and decide to output something if your evaluation is either true or false, it will always only output the evaluation that is true.

This means, if your custom fields values are the same, it would (with above example) output XYZ, otherwise, ABC.

Now, If you want to output BOTH (ABC and XYZ) in ANY case, there is no need to evaluate the Custom Fields values.

wpv-if is used to either do something in case "A" is true, otherwise, do something else.
Just as you say above:
"if they're the same, show abc. If they're different, then show xyz."

I am not sure why you would like to output both values (ABC and XYZ) if you evaluate the Custom Fields, but perhaps I misunderstand the issue here.

Please also note that you did not use the correct Custom Field Names, in the DOC it mentions that you should use the name as it is stored in the Database, which means, with the wpcf- appendix:
https://toolset.com/documentation/views-shortcodes/#wpv-if
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/#checking-custom-field-values > page's bottom

Please, if I misunderstood your query, could you please elaborate a little more so I can better understand?

Sometimes screenshots illustrating what you mean may go a long way to clarify what words fail to transmit, so if you like, make use of "upload an image" below the comment area.

Please let me know if you have further questions regarding the issue mentioned in this Thread
and let me know if the above solution works for you, I look forward to your reply!

Thank you

#298917
fields.a.png
fields.b.png
code1.a.png
code1.b.png
code2.a.png
code2.b.png

Hi beda.s,

Thanks for your response.

1. The below should answer your questions to the 'why' someone would want to show 'both' values.

2. I can't get any more specific/detailed in the subject line because it's exactly what I need to do.

3. I followed the documentation; that's how I managed to make it this far.

4. I'm using non-types custom fields in the content template; that's how they were input into the template using the "fields/view" button, so I'm going to assume the naming is right.

5. The syntax looks right to me and you confirmed it is so something else is wrong. The operators "=" or "!=" are not quite working right, so because of that I thought maybe I had written the wpv-if statement wrong, hence why I'm asking for help. So with more detail, here's what I've got going and trying to do.

Based on this wpv-if statement, I would expect to see 1 post with only a Worldwide date and the other post to show both dates, however, as you can see in the attached images, this is not the case.

 
<div>
[wpv-if r1="[wpv-post-field name="_wpmoly_movie_local_release_date"]" r2="[wpv-post-field name="_wpmoly_movie_release_date"]" evaluate="$r1 = $r2"] Worldwide Release Date: [wpv-post-field name="_wpmoly_movie_release_date"][/wpv-if] 
</div>
<div>
[wpv-if r1="[wpv-post-field name="_wpmoly_movie_local_release_date"]" r2="[wpv-post-field name="_wpmoly_movie_release_date"]" evaluate="$r1 != $r2"] US Release Date: [wpv-post-field name="_wpmoly_movie_local_release_date"] Production Release Date: [wpv-post-field name="_wpmoly_movie_release_date"][/wpv-if] 
</div>

Now, when I flip around the operator using this statement, see the attached images for what I get instead.

 
<div>
[wpv-if r1="[wpv-post-field name="_wpmoly_movie_local_release_date"]" r2="[wpv-post-field name="_wpmoly_movie_release_date"]" evaluate="$r1 != $r2"] Worldwide Release Date: [wpv-post-field name="_wpmoly_movie_release_date"][/wpv-if] 
</div>
<div>
[wpv-if r1="[wpv-post-field name="_wpmoly_movie_local_release_date"]" r2="[wpv-post-field name="_wpmoly_movie_release_date"]" evaluate="$r1 = $r2"] US Release Date: [wpv-post-field name="_wpmoly_movie_local_release_date"] Production Release Date: [wpv-post-field name="_wpmoly_movie_release_date"][/wpv-if] 
</div>

This is why I'm asking for help.

Becky

#298951

Thanks for the Details

I understand you want to check IF a certain Custom Field (Local release Date) is set, then display it as well together with the World Wide Release Date, other wise, display only the World Wide release date.

You could try a different approach:

[wpv-if completed="_wpmoly_movie_local_release_date" evaluate="$completed = 1"]Something[/wpv-if]

https://toolset.com/documentation/user-guides/conditional-html-output-in-views/#introduction-to-conditional-output

For your example, this below should work:
(you may need to adapt it)

<div>
[wpv-if completed="_wpmoly_movie_local_release_date" evaluate="$completed = 1"]US Release Date: [wpv-post-field name="_wpmoly_movie_local_release_date"]Worldwide Release Date: [wpv-post-field name="_wpmoly_movie_release_date"][/wpv-if] 
</div>
<div>
[wpv-if completed="_wpmoly_movie_local_release_date" evaluate="$completed != 1"]Worldwide Release Date: [wpv-post-field name="_wpmoly_movie_release_date"][/wpv-if] 
</div>

This should output, IF the Local release date has a value, the Local AND the WorldWide Release date.
Otherwise, only the WorldWide release Date.

Please let me know if the above solution works for you, I look forward to your reply!

Thank you

#298957

No, that's not working. The end result is the same as what I originally had. The one that has the different meta in the fields is still coming out as 1.

Thanks,
Becky

#298958

Oh, both fields will always have a value, the question is IF the value is the same or not.

#298982
fields.a.1.png
fields.b.2.png

Here's a bit more visual of what I need.

#299023

beda.s,

I figured out the solution and am marking this resolved.

For future reference...when using a non-types custom field in this way you have to use the raw field name -- in my case it's _wpmoly_movie_local_release_date. Adding wpcf- to the front of the field name or injecting it via the "fields and views" button doesn't work.

For some reason, your solution using 'completed' didn't work using the raw field name. I haven't figured out why, it just didn't.

This is what I ended up with that works:

 <div>
[wpv-if r1="_wpmoly_movie_local_release_date" r2="_wpmoly_movie_release_date" evaluate="$r1 = $r2"]Worldwide Release Date: [wpv-post-field name="_wpmoly_movie_release_date"][/wpv-if] 
</div>
<div>
[wpv-if r1="_wpmoly_movie_local_release_date" r2="_wpmoly_movie_release_date" evaluate="$r1 != $r2"]US Release Date: [wpv-post-field name="_wpmoly_movie_local_release_date"] Production Release Date: [wpv-post-field name="_wpmoly_movie_release_date"][/wpv-if] 
               </div> 

Becky