Jump to content

Randomize order in backend (actual order, not presented order)


Recommended Posts

Hi there,

I'm trying to achieve randomizing a large set of pages that needs pagination. It could happen every night or so.

I know sort=random doesn't work with pagination so I thought, if I can randomize the position of the pages in the actual backend this could work. This might be a heavy operation, but I also wouldn't know how to do it so it's effective. There is the https://github.com/kixe/PageMove PageMove. plugin that could to this with the method sortChildren(). This plugin doesn't seem to be present in the store. But, in the forums there is mentions of this function being built in to v 3.0, but I can't find the docs for that.

Would you know the best way to go about this?



Link to comment
Share on other sites

I have ~800 things (in this case published texts) that needs to be shown in random order. It's about 20-50 pages or results, depending how many I show per page.

Preferably the randomization stays the same, so a refresh (or a page change) doesn't ruin the order.



Link to comment
Share on other sites

If you’re running MySql 8 you can use the row_number window function:

update pages p
    inner join (select id, row_number() over (partition by parent_id order by rand()) - 1 as randomsort
                from pages
                where parent_id = :YOUR_PARENT_ID) as r on r.id = p.id
set p.sort = r.randomsort

There may be faster ways to give consecutive sort numbers to random children, but this runs in the blink of an eye (Adminer says 0.005s for my 986 pages). Repeat until you get the order you like.

Make sure you set the parent to “manual drag-and-drop” or you won’t see any effect.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...