1

I have a bunch of testimonials in a channel. Some have the exact same value in the title field.

My code looks like:

{exp:query 
sql="
SELECT DISTINCT title
FROM exp_channel_titles 
WHERE channel_id=19 
ORDER BY RAND() LIMIT 3
"}

    {exp:channel:entries channel="testimonials" disable="categories|member_data|pagination" 
    search:title="{the_title}" dynamic="no" limit="1"}

       {title}

     {/exp:channel:entries}


{/exp:query}

How can I insure I do not get more than one entry with the same title?

I can write a little SQL if needed.

Thx.

  • Hey Ben, if the issue is solved, you need to select the appropriate answer to close out the question. It tells people which answer worked, as well as improves the site stats. – nonprofit_tech Apr 13 '13 at 12:25
1

Updated query:

{exp:query sql="SELECT tmp.entry_id AS entryId
FROM (SELECT entry_id, title FROM exp_channel_titles WHERE channel_id='1' AND status='open' ORDER BY RAND()) tmp 
GROUP BY tmp.title LIMIT 5"}
    {exp:channel:entries entry_id="{entryId}" disable="categories|member_data|custom_fields|pagination|trackbacks}
        <h3>{title}</h3>
    {/exp:channel:entries}
{/exp:query}

You need to pass correct channel_id within WHERE clause.

You can see, I am applying the GROUP BY clause on random records so that query can group the entries based on random order otherwise each time query will return the top most record having duplicate Title.

I hope, it would be workable for you.

| improve this answer | |
  • Thanks - but getting zero results when trying in the CP's database query form. – 4midori Apr 9 '13 at 19:15
  • Plz let me check also. May be, there is something wrong in query. – Bhashkar Yadav Apr 10 '13 at 4:31
  • I updated my query. Can you try it. It would work. – Bhashkar Yadav Apr 12 '13 at 17:35
1
+50

I believe Ian's response is the closest to what you need. I'd modify the query to:

{exp:query sql='SELECT DISTINCT title FROM exp_channel_titles WHERE channel_id='3' ORDER BY RAND() LIMIT 3'}

        {title}

{/exp:query}

I'm not certain why the channel:entries tag was introduced. It's not needed. Now there could be a debate about whether the items are truly random, since the DISTINCT statement is used. But they aren't forbidden from being used together.

So this should work. Make sure, the channel_id matches the id from testimonials.

| improve this answer | |
  • Yes, this query will work but what about if one need to show other channel data with/without title. I believe, there other channel data also be shown. – Bhashkar Yadav Apr 13 '13 at 1:57
  • He only asked for de-duplicating the title. If needs to select from multiple channels, he can just use MySQL' IN function like WHERE channel_id IN(1,3,5) for multiple channels, if he needs them. But based on the questions, this is an issue specific to pulling client testimony from a single channel called testimonials where title and the data field, say testimony have same data. – nonprofit_tech Apr 13 '13 at 4:49
  • For simply fetching non-duplicate titles only your solution is good and easy to use. – Bhashkar Yadav Apr 13 '13 at 10:48
  • 1
    Why are you introducing {exp:channel:entries}? The {exp:channel:entries} tag will perform it's own query, pretty much invalidating the specifics of the both the DISTINCT and RANDOM functions. If you just want 3 random and distinct titles, my query as is (well modifying channel_id to match yours) should work. Adding the {exp:channel:entries] re-queries the database, with it's own parameters. I can't see any reason why'd anyone would add {exp:channel:entries} it to a query function. It must produce a ton of inconsistencies. Is this a standard practice? – nonprofit_tech Apr 16 '13 at 19:11
  • 1
    Just tried the query again, against a database where I introduced duplicate titles and expanded the search across three sections, which I know ail include more duplicates, then set return to 50 records at a time (the database has over 4000 records. Ran the query 10 times, not a single duplicate title. Try it without introducing {exp:channel:entries}. I'm not certain of your reasons for adding it, but it ignores the returns from the original query and introduces it's own. – nonprofit_tech Apr 16 '13 at 20:59
0

If you happen to already be using Stash, you can probably achieve this with the set_list and get_list methods in combination with the unique="yes" parameter.

If you're not already using Stash, then the query module approaches would be best.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.