• I’m not a developer, but I generally can manage to snoop around and frankenstein together code… so forgive me if this is a stupid question.

    My site is a tweaked version of the Pronto theme. The posts are basically an image with a title and its tags (just realized that sounds like an adult site, but it’s not, ha). They number in the hundreds. In the sidebar, I’d like to have a “Sort by newest” button and a “Random order” button that function on the main page as well as when viewing only a category or a tag.

    Unfortunately I can’t post a link because it’s tied to a future release date.

    I’ve found some help on the forums with randomization of posts, but they don’t seem to apply to what I’m specifically hoping to accomplish. Is my goal particularly complicated to achieve?

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    It can be pretty basic, or somewhat complex, depending on your desired user experience and if pagination is part of the equation. The basic way would be that the button is simply a link that reloads the page, but with an URL parameter appended indicating the selected sort order.

    In PHP, hook ‘pre_get_posts’ and set the appropriate sort query var based on the passed parameter. When selecting random search order, pagination will not work because the post order returned must be reproducible. For random pagination, you have to query for all posts and manage what goes on what page with PHP instead of SQL offsets. Or perhaps keep track of what random posts have been sent out so subsequent random queries will not contain duplicates.

    You can enhance the user experience by using AJAX or maybe just javascript to rearrange the posts on the page without actually reloading the page. Pretty cool stuff, but probably difficult to cobble together from random snippets found with Google.

    Another enhancement that might be possible by cobbled script is retaining the user preference, either as user meta or in a cookie.

    There are choices even for how the buttons appear on the sidebar. You could use the link widget, styled like a button. You could create a custom widget, with enhancements like only showing the button opposite of the current sort order. Or simply put the button code on your sidebar template and skip using a widget for this. It will need to be either before or after the other widgets though. It needs to be a widget to be mixed in with the rest of them.

Viewing 1 replies (of 1 total)
  • The topic ‘sort posts by date or random – controlled by "toggle" in sidebar?’ is closed to new replies.