Skip Navigation

[Resolved] How to display 3 random media posts?

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to display 3 random images from the media gallery?

Solution:
Create a View which queries the media post type with a limit of 3 and random order, as shown in the screenshot in the reply below: https://toolset.com/forums/topic/using-views-to-create-randomized-image-galleries-from-the-media-bin/#post-624240

An oddity of the media post type is that it is not necessary to include anything in the Loop Output section, the images will be added automatically by WordPress.

This support ticket is created 6 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 2 replies, has 2 voices.

Last updated by Stephen 6 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#624099

Tell us what you are trying to do?
Essentially, I am trying to create a View that queries the media bin or a CPT (however this can get done), and displays it wherever the view is deployed.

Is there any documentation that you are following?
Just the Views documentation

Is there a similar example that we can see?
hidden link if you scroll the bottom of the page, you'll see images that are randomized. If you refresh the screen the images will update with another set of randomized images, creating a gallery each time.

What is the link to your site?
hidden link

#624240

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Screen Shot 2018-03-12 at 10.51.43.png

Hi Stephen

You can create a View which queries the Media (which is just another post type, albeit a built-in WordPress one) and which limits the results to just 3 images, randomly ordered, as shown in my screenshot.

While setting up a demo to generate that screenshot, I found an oddity of such a View is that it will output the image inside a link element automatically, even if you don't insert anything in the Loop Output section.

So you can create such a View, and use the Loop Wizard to render the output in a certain number of columns, e.g. 3. The images will be added automatically.

That does mean that any of your media gallery images might show up, and that might not be what you want.

In which case you would make a custom post type, e.g. called slides, and add an image custom field to the slide posts.

Then create a View, much like above, that displays 3 random slide posts, and in the Loop Output section output the image field.

Let me know if you have problems with that.

#624472

Hi Nigel,

Thank you so much for the idea & response. I will give it a go. If I have any issues, I'll reach out again!

be well,
Stephen

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.