Skip Navigation

[Resolved] Conditional Logic Issues

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a View that shows a CPT. The CPT has a custom field date that stores an event end date. I would like the View to show the post with a date custom field value that is coming up next. If no matching post is found, I would like to display some other default content.

Solution: Add a custom date field Query Filter and configure it to show posts with upcoming end dates greater than or equal to TODAY. Add sorting based on this custom field value, ascending, so the next upcoming end date is first. Then set a limit of 1 post, so only the first event will be shown. In the Loop Editor, place your event content in the wpv-loop tags. Place your default content in the "No items found" area.

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
https://toolset.com/documentation/user-guides/date-filters/

This support ticket is created 6 years 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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Tagged: 

This topic contains 5 replies, has 2 voices.

Last updated by jasonw-2 6 years ago.

Assisted by: Christian Cox.

Author
Posts
#1160085

I have made a CPT for a featured message on the homepage. What I'm trying to do within a view is to add conditional logic to the view template where it will display the featured message, or default to the standard content that should show in that block if the (wpcf-upcoming-end-time) date field has passed.

Here's my disaster or code:

[wpv-conditional if="( $(wpcf-upcoming-end-time) gte 'TODAY()' )"]
<div class="row">
	<div class="col-sm-8"><h1 style="color:#ae11bc; font-size:22px; font-weight:bold; text-align:center; font-family: Oxygen;">[types field='upcoming-title'][/types]</h1>
    <p style="color:#4054b2; font-size:17px; text-align:center; margin-top:-14px; font-weight:bold;">[types field='upcoming-sub-title'][/types]<p>
    <h3 class="paragraph3">[types field='upcoming-paragraph'][/types]</h3>
    <p style="text-align:center; margin-top:0px;">[types field='upcoming-link' title="Click here for more information"][/types]</p>
</div>
	<div class="col-sm-4"><div class="image"><img src="/wp-content/uploads/announcement.jpg" width="205px"></div></div></div>
[/wpv-conditional]

[wpv-conditional if="( $(wpcf-upcoming-end-time) lt 'TODAY()' )"]
<div>
  <h1 style="text-align:center; font-size:2em; margin-top: -20px; margin-bottom: 5px;"> <span style="color:#000;">OFFER</span> <span style="color:#c0f; font-weight:bold;">HELP?</span></h1>
  <hr style="width:200px; border-color:rgba(138,80,224,0.66); border-style:solid; margin: 0 auto;">
  <h2 style="text-align:center; color:#47c6b1; padding-top:2px; margin-bottom:2px;">It's easy to make a difference</h2>
  <p style="color:#4054b2; font-style:italic; text-align:center;">For any community, <strong>Domestic & Sexual Violence</strong> can easily end up being an "Out of sight, out of mind" situation. While we do our best to let you know what we're up to, sometimes our message can slip through the cracks. Please help us <strong>continue our fight against Domestic & Sexual Violence</strong> by making a <span style="text-decoration: underline;">small monthly donation</span>. It's easy to make a difference!</p>
  
<div style="max-width:330px; padding: 0 2px;  display: flex; justify-content:center; background-color: #ffe3ff; margin: 0 auto; "><a href="/donations/">Make a monthly donation </a> | <a href="/get-involved/">Get Involved</a></div></div>[/wpv-conditional]

I have it sorted to where if there's more than one 'featured' post, that they order themselves correctly and only show one but to save my life I cannot figure out how to show the second 'standard content' when the date has passed for the last featured message. If I take the conditional logic out of the normal content code, it just shows the "No items found" which now says "Working on this.." on the homepage.

What am I doing wrong or how do I make this happen? I've spend 6 hours trying to figure out as I hate to bother you all. Lol.

Thanks for your help!! 🙂 (and seriously.. just try and overlook my inline css disasters and my coding in general). 🙂

#1160094

Please provide login credentials in the private reply fields here. Let me know where I can see the View on the front-end of your site, and I'll take a closer look.

#1160111
Screen Shot 2018-12-05 at 3.09.46 PM.png

Looks like there is a limit of 1 result imposed in the View editor. That's why only one post is showing up. Please remove the limit or set it to a different number.

#1160129

I was hoping to make it to where even if there are several posts made ahead of time, that it would show 1 at a time (closest one to expiring first) and that if all of them had that date field where it was expired, that it would just show the standard content.

With the code below, It's showing the standard content but there's a post that that expires on the 22nd of December so it's still not doing what I'd hoped. *Where we were is that it was showing the 'valid' features but would not show the standard content at all. That's where it was having no results.

[wpv-conditional if="( $(wpcf-upcoming-end-time) gte 'TODAY()' )"]
<div class="row">
	<div class="col-sm-8"><h1 style="color:#ae11bc; font-size:22px; font-weight:bold; text-align:center; font-family: Oxygen;">[types field='upcoming-title'][/types]</h1>
    <p style="color:#4054b2; font-size:17px; text-align:center; margin-top:-14px; font-weight:bold;">[types field='upcoming-sub-title'][/types]<p>
    <h3 class="paragraph3">[types field='upcoming-paragraph'][/types]</h3>
    <p style="text-align:center; margin-top:0px;">[types field='upcoming-link' title="Click here for more information"][/types]</p>
</div>
	<div class="col-sm-4"><div class="image"><img src="/wp-content/uploads/announcement.jpg" width="205px"></div></div></div>
[/wpv-conditional]

[wpv-conditional if="( $(wpcf-upcoming-end-time) lt 'TODAY()' )" evaluate="false"]
<div>
  <h1 style="text-align:center; font-size:2em; margin-top: 5px; margin-bottom: 5px;"> <span style="color:#000;">OFFER</span> <span style="color:#c0f; font-weight:bold;">HELP?</span></h1>
  <hr style="width:200px; border-color:rgba(138,80,224,0.66); border-style:solid; margin: 0 auto;">
  <h2 style="text-align:center; color:#47c6b1; padding-top:0px; margin-bottom:2px;">It's easy to make a difference</h2>
  <p style="color:#4054b2; font-style:italic; text-align:center;">For any community, <strong>Domestic & Sexual Violence</strong> can easily end up being an "Out of sight, out of mind" situation. While we do our best to let you know what we're up to, sometimes our message can slip through the cracks. Please help us <strong>continue our fight against Domestic & Sexual Violence</strong> by making a <span style="text-decoration: underline;">small monthly donation</span>. It's easy to make a difference!</p>
  
<div style="max-width:330px; padding: 0 2px;  display: flex; justify-content:center; background-color: #ffe3ff; margin: 0 auto; "><a href="/donations/">Make a monthly donation </a> | <a href="/get-involved/">Get Involved</a></div></div>
#1160134

Okay please check now. I removed all the conditionals, because they are not necessary. In the View, I added a Query Filter such that only results that end after today will be displayed. If no results are found, then the default content you originally had in the second conditional will be displayed, because I moved it into the "no results found" area of the View editor.

#1160136

You are the man. Works perfect and I learned something. Thanks, man!! 😀