If loop exists else

Hi everyone,

I am looping through some posts and a want to display the title if the ticket category is equal to “task”, this is currently working except my else statement is fired off as well so I see the 2 tasks I have added along with the “No tasks added”.

<Loop type=wcrm_ticket_activity custom_field=wcrm_tickets_activity_ticketid custom_field_value="{Field id}" sort_field=publish_date sort_order=desc>
  <div class="updatedActivityItem">
   <If field=wcrm_ticket_category value=task>
     <div class="taskList">
       <h2><Field title /></h2>
       <Field content />
     </div>
   <Else />
     No tasks added
   </If>
  </div>
</Loop>  

Thanks,

Tom

Hi Tom! Based on what I understand from your template, you have a CPT called wcrm_ticket_activity and that post type has a field called wcrm_tickets_activity_ticketid which, for some reason, is the same number/string as the post id. Is that correct? I’m a bit confused as to why you’d have a wcrm_tickets_activity_ticketid field and an id field that are identical and why you’d filter your loop based on that field, but maybe it’s just early and my brain hasn’t quite turned on this morning haha!

I’ve got two ideas you might want to look into. The first is the If loop exists syntax. With this syntax, it just checks to see if there are any items in a loop and if there are, then it loops through them, but if there aren’t, it just displays the messages after the Else tag. This is in contrast to your syntax above where the “No tasks added” message could be displayed multiple times in instances where there are multiple posts in the loop but none of them match the conditional logic you’ve set up.

The second idea is that you might want to filter your initial loop using the custom_field parameter and the custom_field_2 parameter to only show posts where multiple custom fields match both of those values you’re looking for (basically just adding custom_field_2=wcrm_ticket_category custom_field_value_2=task to your loop’s query parameters). You may have already read through this but you can check out the filtering the loop section of the docs for an explanation of the various ways to filter down your loop to only show certain results.

Hope that helps!

Hi Benjamin,

Thank you for the response, I’m taking over building out a system and just learning Tangible, I’m a PHP dev usually.

I need both of them because a ticket activity id is linked to another CPT called tickets which the custom_custom_field_value="{Field id}" holds the ID which links this.

Thank you for the help with the if loop exists, this is how I have got it working.

<div class="updatedActivty">
  <If loop exists type=wcrm_ticket_activity custom_field=wcrm_tickets_activity_ticketid custom_field_value="{Field id}" sort_field=publish_date sort_order=desc>   
    <Loop>  
    <div class="updatedActivityItem">
     <If field=wcrm_ticket_category value=task>
       <div class="staffChatList">
         <h2><Field title /></h2>
         <Field content />
       </div>
      </If>
    </div>
    </Loop> 
  <Else />
   No messages available.       
 </If>
</div> 

Is this a good way to work, eg putting the loop info into an if statement?

Right of course, that makes sense, the caffeine must have just not kicked in yet when I first read your message. Glad you got it working! I’d still recommend that you check out filtering your query with custom_field_2 instead of using a nested If tag since I think it should work in your case and that approach would be slightly more efficient and would allow you to use features like loop pagination in the future if the need arises. But I assume you don’t have a ton of tickets so it’s not really a big deal either way. Just want to make sure you’re getting the most performance out of the plugin! Happy looping :slight_smile:

Haha no problem, I know the feeling, sometimes just needs an extra 5 minutes to kick in :slight_smile:

Sorry I missed that part with the custom_field_2 I will have a look at getting that to work now as there will be a lot of tickets in future but for now there isn’t.

Thanks Benjamin

1 Like

Hi @benjamin

Can you help explain why the following is not display the Else statement please, I am now using the custom_field which brings back everything correctly but if there are no results the nothing is displayed.

<div class="updatedActivty">
    <Loop type="wcrm_ticket_activity" custom_field=wcrm_tickets_activity_ticketid custom_field_value="{Field id}" custom_field_2=wcrm_ticket_category custom_field_value_2=chat>
      <If loop exists type=wcrm_ticket_activity>
        <div class="updatedActivityItem">
          <div class="staffChatList">
           <Field title />
         </div>
        </div>
      <Else />
        No messages.
      </If>
    </Loop>
</div>

Thanks, long day so apologies if I have missed anything.

Take a look at these two lines:

<Loop type="wcrm_ticket_activity" custom_field=wcrm_tickets_activity_ticketid custom_field_value="{Field id}" custom_field_2=wcrm_ticket_category custom_field_value_2=chat>
  <If loop exists type=wcrm_ticket_activity>

The first line creates a loop of posts in your wcrm_ticket_activity CPT and filters it to only show posts where certain custom fields match certain values.

The second line checks whether a loop exists where the only parameter is type=wcrm_ticket_activity. Basically, for each post in the outer loop, you’re checking whether there are any wcrm_ticket_activity posts at all, which you already know there are because you’re looping through them. If there aren’t any wcrm_ticket_activity posts, then your outer loop wouldn’t even run once, meaning that the conditional logic within that outer loop wouldn’t even get a chance to run. That’s why it’s not displaying your “No messages.” message.

You had it right in your earlier message where it should look something like this:

<div class="updatedActivty">
  <If loop exists type="wcrm_ticket_activity" custom_field=wcrm_tickets_activity_ticketid custom_field_value="{Field id}" custom_field_2=wcrm_ticket_category custom_field_value_2=chat>
    <Loop>
      <div class="updatedActivityItem">
        <div class="staffChatList">
         <Field title />
       </div>
      </div>
    </Loop>
    <Else />
    No messages.
  </If>
</div>

Ah @benjamin thank you very much, that just clicked something in my brain after your post, of course it wouldn’t run. I now understand the Loop and If statements in L&L a lot more.Brain has definitely melted today.

I appreciate your time and help, thank you.

1 Like

Haha, I’ve had plenty of brain-melting moments myself with L&L. Glad that all makes sense and you’ve got it working now!

1 Like