Sitemap

Wix Data Random Sorting: Complete Solution

3 min readDec 14, 2021

--

Hey, are you looking for a way to randomise the Wix data results?

Admirable 👍 You find the right place.

Our team just made it work.

Here is how we did (Logic of “Randomise”):

  1. Save a random value to each item.
  2. Sort the random value field by A-Z (or Z-A if you want) on Wix dataset settings.
  3. Done ✅

Ok, imagination always looks good. 😃

But how can we get this work?

You can, if you know how to code (or copy and paste at least)

Steps:

1: Enable Velo Dev Mode

Press enter or click to view image in full size
Here is where you can find the “Dev Mode.”

2: Create backend code files, “data.jsw” and “jobs.config”.

3. Paste the code below into “data.jsw”, then replace your collection ID.

To find your collectionId, select the Databases tab in the Velo Sidebar. Hover over your collection, click the three dots, and select Edit Settings.

import wixData from 'wix-data';const makeid = () => {let id = '';const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';const nums = '0123456789'for (let i = 0; i < 2; i++) {id += chars[Math.floor(Math.random() * chars.length) + 1]}for (let i = 0; i < 3; i++) {id += nums[Math.floor(Math.random() * nums.length) + 1]}return id}export async function refresh() {const databaseId = "YOUR_DATABASE_ID"console.log("HEX START")let results = await wixData.query(databaseId).limit(1000).find()for (let index = 0; index < results.items.length; index++) {results.items[index].hex = makeid();}await wixData.bulkSave(databaseId, results.items, { suppressAuth: true })console.log("HEX SUCCESS")}

4. Create a field with the field key “hex” on the collection.

Get Discover by Certified Code’s stories in your inbox

Join Medium for free to get updates from this writer.

5. Setup jobs that run every X minutes. Paste the following code below to “jobs.config”

{"jobs": [{"functionLocation": "/data.jsw","functionName": "refresh","executionConfig": {"cronExpression": "0 * * * *"}},{"functionLocation": "/data.jsw","functionName": "refresh","executionConfig": {"cronExpression": "3 * * * *"}},{"functionLocation": "/data.jsw","functionName": "refresh","executionConfig": {"cronExpression": "6 * * * *"}},{"functionLocation": "/data.jsw","functionName": "refresh","executionConfig": {"cronExpression": "9 * * * *"}},{"functionLocation": "/data.jsw","functionName": "refresh","executionConfig": {"cronExpression": "12 * * * *"}},{"functionLocation": "/data.jsw","functionName": "refresh","executionConfig": {"cronExpression": "15 * * * *"}},{"functionLocation": "/data.jsw","functionName": "refresh","executionConfig": {"cronExpression": "18 * * * *"}},{"functionLocation": "/data.jsw","functionName": "refresh","executionConfig": {"cronExpression": "21 * * * *"}},{"functionLocation": "/data.jsw","functionName": "refresh","executionConfig": {"cronExpression": "24 * * * *"}},{"functionLocation": "/data.jsw","functionName": "refresh","executionConfig": {"cronExpression": "27 * * * *"}},{"functionLocation": "/data.jsw","functionName": "refresh","executionConfig": {"cronExpression": "30 * * * *"}},{"functionLocation": "/data.jsw","functionName": "refresh","executionConfig": {"cronExpression": "33 * * * *"}},{"functionLocation": "/data.jsw","functionName": "refresh","executionConfig": {"cronExpression": "36 * * * *"}},{"functionLocation": "/data.jsw","functionName": "refresh","executionConfig": {"cronExpression": "39 * * * *"}},{"functionLocation": "/data.jsw","functionName": "refresh","executionConfig": {"cronExpression": "42 * * * *"}},{"functionLocation": "/data.jsw","functionName": "refresh","executionConfig": {"cronExpression": "45 * * * *"}},{"functionLocation": "/data.jsw","functionName": "refresh","executionConfig": {"cronExpression": "48 * * * *"}},{"functionLocation": "/data.jsw","functionName": "refresh","executionConfig": {"cronExpression": "51 * * * *"}},{"functionLocation": "/data.jsw","functionName": "refresh","executionConfig": {"cronExpression": "54 * * * *"}},{"functionLocation": "/data.jsw","functionName": "refresh","executionConfig": {"cronExpression": "57 * * * *"}}]}

6. Done.

7. Bonus: If you want to get the database refreshed everytime user visits the page, add the following code to the page code/masterpage.js

import { refresh } from 'backend/data.jsw'$w.onReady(function () {refresh()});

Done!

If it works: Please like and comment!

If it does not work: Please comment with the details below!

If you are looking for someone to do it for you, don't hesitate to get in touch with team@certifiedcode.us

--

--