How to display one random item from a multiple-select relationship field in Pods
This lesson covers a specific scenario with the Pods framework in WordPress where you have two custom post types linked by a multiple-select Relationship field with multiple values. This example describes a Service post type and a Testimonial post type, where you can have multiple Testimonials linked to one Service, and you want to display just one of those testimonials at random each time the Service page is loaded.
The WordPress/Pods configuration
- I have two Custom Post Types, both created with Pods, called Service and Testimonial.
- I want to be able create a Service post, link one or more testimonials to that service, and then display one of those testimonials at random each time that Service post is viewed.
- I created a bi-relationship field to link Service and Testimonal together, and allowed multiple values.
- This enables me to create a Testimonial post and then choose one or more Services to link it to.
- It also enables me to create a Service post and then choose one or more Testimonials to link to it. In this case I can also create new Testimonials rather than having to create them first before editing my Service post.
- When the Service post is viewed, it should show one of those testimonials at random. When reloading the page, another testimonial would be chosen at random and displayed.
The Solution
There are two basic steps to achieve the objective in this scenario.:
- You need to create a custom PHP function that will select one testimonial at random and format it ready for display on the page.
- You then need to embed a Pods shortcode in your site. This shortcode will include a Pods Magic Tag and will reference the custom PHP function from step 1.
Here's the PHP function from this example, along with some extra comments to help you understand it. You can place this function in the functions.php file in your site's theme. If you prefer, you could put it in a custom WordPress plugin.
Then, I added a shortcode to the Service pages in my site:
[pods]{@id,one_random_testimonial}[/pods]
This resulted in one testimonial from the selection available being displayed below a heading saying "Happy Customers". As required, reloading the page results in a different testimonial being loaded each time (although sometimes the same testimonial may be chosen again the next time the page loads, it is definitely happening randomly).
As a extra for anyone using Beaver Themer, you can use the Pods connection in Beaver Themer to enter the Pods magic tag into your Beaver Themer layouts (in my case, this was a layout that was used for the Service post type). In this case, simply paste the field rather than the short code, i.e. {@id,one_random_testimonial}.
A big thanks to Jim True of the Pods team who helped me work through this scenario. Find out more about Pods for WordPress and how you can use it create custom post types, custom fields, and relationships between post types on the Pods website.
Join our mailing list
Our Comment Policy.
We welcome your comments and questions about this lesson. We don't welcome spam. Our readers get a lot of value out of the comments and answers on our lessons and spam hurts that experience. Our spam filter is pretty good at stopping bots from posting spam, and our admins are quick to delete spam that does get through. We know that bots don't read messages like this, but there are people out there who manually post spam. I repeat - we delete all spam, and if we see repeated posts from a given IP address, we'll block the IP address. So don't waste your time, or ours. One other point to note - if you post a link in your comment, it will automatically be deleted.
Add comment