Grid View Random Entry Display

By: scott davis | Asked: 10/17/2022
ForumsCategory: How-toGrid View Random Entry Display
scott davis asked 2 years ago

Hey, I'm looking for a good way to take the grid view of a directory form entry and get it to show random entries from the form.  I think I can do it with this code snippet example, but... I don't know how to tell it to give me a random ordering. I get the ascending and descending part, but how do I tell it to sort randomly?  I'm also wanting this to resort when the page reloads, if that saves a question. I was assuming the random in itself would shuffle automatically on a page load.

add_filter('frm_view_order', 'change_view_order', 10, 2);
function change_view_order($query, $args){
    if ( $args['display']->ID == 2412 ) { //replace 2412 with you Field ID
        $query['order'] = 'ORDER BY MONTH(it.created_at) ASC';
    }
    return $query;
}


 

Attachments
2 Answers
Best Answer
Chris AdamsChris Adams answered 2 years ago
Hi Scott,   If you just want to randomise the entries shown in a view you can do that without custom code.   In the 'Sort Entries' section, at the bottom of the list is an option for 'Random' which will randomise the order the entries are shown. You can combine this with the 'Limit' setings to only show a set number of random entries if you wish.   Hope that helps   Chris
scott davis replied 2 years ago

That's exactly what I was looking for. I'm a bit surprised their support didn't start there instead of web hooks...

Thanks. I never would have looked under the left drop down... kept trying to figure out why it wasn't an option on the right side drop down.

Victor Font answered 2 years ago
Whenever you're customizing a MySQL query, it's best to test your ideas in PHPMyAdmin first. Once you write a query that works the way you want, then you could transfer the working code to Formidable. There are multiple ways to generate SQL select statements where the results are in a random order. Here are a few of them that use the SQL RAND() function: https://www.mysqltutorial.org/select-random-records-database-table.aspx

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right