user1070143
user1070143

Reputation: 427

Is this the right way to filter channel entries using conditional variables?

Is this the most efficient way to filter a load of channel entries? I want to display entries that have no comments and that are not sticky. I'm using this code.

{exp:channel:entries channel="{segment_3}" status="open" orderby="date" disable="categories|category_fields|member_data|pagination"}

  {if comment_total == "0" AND sticky == 'n'}
    ...
  {/if}

{/exp:channel:entries}

Cheers Lee

Upvotes: 2

Views: 749

Answers (2)

Alex Kendrick
Alex Kendrick

Reputation: 969

Using conditional variables, probably. But this will likely return lots more results than you need. Plus you won't be able to accurately use {count} (maybe not an issue for you, though).

Another approach which doesn't use conditional variables, but just goes straight after the results you want, and only the results you want, is to use the Query Module

{exp:query sql=
    "SELECT title
    FROM exp_channels
    JOIN exp_channel_titles ON exp_channels.channel_id = exp_channel_titles.channel_id
    WHERE exp_channels.channel_name = '{segment_3}'
    AND exp_channel_titles.status = 'open'
    AND exp_channel_titles.sticky = 'n'
    AND exp_channel_titles.comment_total = 0"
}
    <li>{title}</li>
{/exp:query}

This could get tedious if you needed to access a bunch of custom fields, but it is an efficient way to get the results you want.

Upvotes: 3

Jean St-Amand
Jean St-Amand

Reputation: 793

Sticky is an available parameter on the entries loop, so you could filter at least that element in the entries loop itself by simply adding sticky="no", but comments, unfortunately, is not an available parameter, so Alex's suggestion may be the best option if your requirements are fairly simple. If you need access to a significant number of custom fields, however, it may be a bit tricky. So you'll have to decide what approach to take based on what you need within your loop.

Upvotes: 0

Related Questions