1

I have searched high and low for a solution and couldn't find anything; hopefully someone here more skilled than myself can lend a hand.

I have two testimonial videos (from a video custom post type) displayed randomly every time the page loads. There are different clients that have testimonials and in some cases, there are multiple testimonials from the same client.

I have the query working that randomly displays two videos from the custom post type (while filtering out a category completely). What I'm trying to avoid is loading two testimonial videos from the same client (looks odd to have two of the same thumbnails show) but I'm not sure how to do this. Here the working code that I have so far:

<?php  $loop = new WP_Query(array(
    'post_type' => 'video',
    'posts_per_page' => 2,
    'orderby' => 'rand',
    'tax_query' => array(
        array(
            'taxonomy' => 'video_category',
            'field' => 'slug',
            'terms' => 'assess-core-certification',
            'operator' => 'NOT IN')
        )
    ));
?>

<?php if ( $loop ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>

<div  class="testimonialVideo top">

    <div class="testimonialVideo-video"><?php the_content(); ?></div>

    <div class="testimonialVideo-title">
        <strong><?php echo get_post_meta( get_the_ID(), 'video_clientName', true ) ?></strong><br/>
        <span class="small"><?php echo get_post_meta( get_the_ID(), 'video_clientCompany', true ) ?><br/><br/>
                        "<em><?php echo get_post_meta( get_the_ID(), 'video_shortQuote', true ) ?></em>"</span>
    </div>

</div>
<?php endwhile; else: ?>

<p class="error-not-found">Sorry, no testimonials found.</p>

<?php  endif; wp_reset_postdata();?>
3
  • From my understanding, you want to query only unique video_clientName meta values, right? So that 2 of the same video_clientName do not show up in your list?
    – Howdy_McGee
    Commented Dec 13, 2013 at 16:59
  • Yes, all while running the working query that I posted above. I'm not sure how to combine the two.
    – theGscott
    Commented Dec 13, 2013 at 17:02
  • If you don't want to run your own SQL Query then @Nick answer would be the best, get a large number of posts and pull 2 unique ones from it. Otherwise you would need to make your own SQL statement and run it through $wpdb to pull DINSTINCT values.
    – Howdy_McGee
    Commented Dec 13, 2013 at 18:03

2 Answers 2

0

It seems like you need to actually prevent two specific VIDEOS from displaying on the same screen (not preventing videos from the same CLIENT twice), but if I'm wrong you can still adapt this. The idea is to push each VIDEOS identifier (name, id, whatever) to an array as you output it, checking each item against the array to see if it is already displayed on the page. If it is, skip it, if not - display it an add it to the list to prevent displaying again.

This is what I did to avoid dupes for a randomly displayed advertisement plugin, and had a global variable containing image ids which I checked against each time an ad item was chosen to be randomly displayed.

Other options include using meta_query (see WP_Query docs in codex) and a custom SQL query, both of which seem overly complex in my opinion. I'm not sure WP_Query can return unique results in a set of metadata or not.

Your code adapted (I haven't tested this code):

<?php $test_meta_list = array(); // array to hold item names that have been displayed ?>
<?php if ( $loop ) : while ( $loop->have_posts() ) : $loop->the_post();

// Get the video meta
$test_meta = get_post_meta( get_the_ID(), 'video_clientCompany', true )

// If it has already been output, skip it
if ( in_array($test_meta, $test_meta_list) ) 
  continue();
// If not, add it to the list and output it
else {
  array_push($test_meta_list, $test_meta)
?>

<div  class="testimonialVideo top">
 <div class="testimonialVideo-video"><?php the_content(); ?></div>
   <div class="testimonialVideo-title">
     <strong><?php echo get_post_meta( get_the_ID(), 'video_clientName', true ) ?></strong><br/>
     <span class="small"><?php echo get_post_meta( get_the_ID(), 'video_clientCompany', true ) ?><br/><br/>
     "<em><?php echo get_post_meta( get_the_ID(), 'video_shortQuote', true ) ?></em>"
     </span>
   </div>
 </div>
</div>
<?php } endwhile; else: ?>  
6
  • The problem with this is that you would have to query a large number of the posts to make sure that you have 2 distinct posts. I'm assuming they have multiple videos from the same client.
    – Howdy_McGee
    Commented Dec 13, 2013 at 17:40
  • In need to prevent 2 videos from the same client from displaying. Some clients have up to 3 or 4 videos. I just need to randomly pull two videos on page load (like my code is currently) without showing two of the same client. I really appreciate all of the help!
    – theGscott
    Commented Dec 13, 2013 at 17:54
  • @Howdy_McGee - that is true, but may or may not be an issue. @ theGscott - why not grab a list of CLIENTS and choose from those randomly, then query VIDEOS based on only the CLIENTS you selected? Commented Dec 13, 2013 at 17:56
  • Looking at the code in the question, CLIENTS are set via string meta value video_clientName - so it is most likely set by the same AUTHOR.
    – Howdy_McGee
    Commented Dec 13, 2013 at 17:59
  • If there is no client->video relationship besides the meta value VIDEO_CLIENTNAME, then the meta value is the only thing you can use to verify duplicate clients. Custom SQL or modification of meta_query like @Howdy_McGee is working on seems like the way to go if you don't want to return all the videos first Commented Dec 13, 2013 at 18:14
0

Piggybacking off of @Nick answer. What this will do it query ALL video posts in that category, checks if client1 == client2 and if it does, show the post and increment how many clients we have showing. The loop should stop after 2 posts are shown and drop out.

Ideally you would want to run an SQL query to do this because of overhead but I don't have enough SQL experience, so you don't want me to write the query for you xD

<?php
$loop = new WP_Query(array(
        'post_type' => 'video',
        'posts_per_page' => -1,
        'orderby' => 'rand',
        'tax_query' => array(
            array(
                'taxonomy' => 'video_category',
                'field' => 'slug',
                'terms' => 'assess-core-certification',
                'operator' => 'NOT IN')
        )
    )
);
?>



<?php
$prevClient = '';
$clients = 0;
if ($loop) :
    while ($loop->have_posts() && $client < 2) :
        $loop->the_post();
        $clientUpper = get_post_meta(get_the_ID(), 'video_clientName', true);
        $clientLower = strtolower($client);
        if ($prevClient != $clientLower) :
            $clients++;
            $prevClient = $clientLower;
            ?>

            <div class="testimonialVideo top">

                <div class="testimonialVideo-video"><?php the_content(); ?></div>

                <div class="testimonialVideo-title">
                    <strong><?php echo clientUpper; ?></strong><br/>
                    <span class="small"><?php echo get_post_meta(get_the_ID(), 'video_clientCompany', true) ?><br/><br/>
                "<em><?php echo get_post_meta(get_the_ID(), 'video_shortQuote', true) ?></em>"</span>
                </div>

            </div>

        <?php endif; endwhile; else: ?>

    <p class="error-not-found">Sorry, no testimonials found.</p>

<?php endif; wp_reset_postdata();?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.