Search form

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

  1. I have two Custom Post Types, both created with Pods, called Service and Testimonial.
  2. 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.
  3. I created a bi-relationship field to link Service and Testimonal together, and allowed multiple values.
    1. This enables me to create a Testimonial post and then choose one or more Services to link it to.
    2. 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.
  4. 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.:

  1. You need to create a custom PHP function that will select one testimonial at random and format it ready for display on the page.
  2. 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.

Important: You will need to modify this function to match your requirements. I've identified the points in the code where this will be required. A lot of the code below is actually comments (lines beginning with //) so it's actually as daunting as it looks.
function one_random_testimonial($id) {
// Get the ID for the current Service post.
// The first value, service, is the name of my custom post type.
// Make sure you change this to match the name of your post type.
$pod = pods( 'service', get_the_id());

// Get the content from the relationship field
// The name of my field in the Service post type that links to the Testimonial
// post type is called related_testimonial. Make sure you change this code to match yours
$related = $pod->field( 'related_testimonial' );

// This code loops through the related testimonials
// but only if there is anything to loop through
// There are actually two loops. This one counts the number of posts
// and generates a random number which will be used to select the testimonial to display
if ( ! empty( $related ) ) {
// Set variables to be used to choose one post randomly
$random=0;
$selected=0;
// Count the number of posts - part of the random selection
foreach ( $related as $rel ) {
$count++;
}
// Generate a random number for choosing the testimonial to display
// If you have 5 related testimonials, this number will be between 1 and 5.
$random=rand(1, $count);

// Then, we repeat the loop through the related testimonials
// and select the one that matches our random number
// If the random number we selected above was 3, this loop will only
// output the testimonial it finds on the third time through the loop
foreach ( $related as $rel ) {
$selected++;
// Check if the current related post is the one to output, otherwise do nothing
if ( $selected == $random) {
$id = $rel[ 'ID' ];
// Output the chosen testimonial
// This code formats the testimonial for display on the page
// You may want to customise this to suit your requirements
// In this case, the testimonial output consists of two fields - the content and the title.
echo '<div class="tst.rnd">';
echo '<h2 class="tst.intro">Happy Customers</h2>';
echo '<div class="tst.content">'.get_post_field('post_content', $id).'</div>';
echo '<div class="tst.name">'.get_the_title( $id ).'</div>';
echo '</div>';
}
}
}
}

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.

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 a comment to this lesson

Add comment