Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Pull unique image from CPT WordPress

I have three circles that appear on a page, and they're pulling an image from a custom post type I have. The goal is to not have the same image appear on the page more than once, and even though I'll have over 200 of these custom posts, murphy's law will make that happen all of the time.

Here's my current code that is pulling an image - everything is working fine. If there's a way to rework this to force them to be unique, that's what I'm after.

I'm attaching an image to give a visual, so it's easier to understand:



<!-- circle two -->

<?php

global $post;

if ( post_type_exists( 'featured-products' ) ) {

$testimonial_query = new WP_Query( array(
'post_type' => 'featured-products',
'orderby' => 'rand',
'posts_per_page' => -1
) );

if ( $testimonial_query->have_posts() ) {
$random_int = rand( 0, $testimonial_query->post_count - 1 );
$post = $testimonial_query->posts[$random_int];
setup_postdata( $post );

echo '<div class="find-og-circle-two"><img src="';
echo 'http://dev.oilygurus.com/wp-content/uploads/2015/12/sandi-boudreau.jpg';
//the_field('oil_image');
echo '"></div>';
}
// Restore original post data
wp_reset_postdata();
}

?>

<!-- circle three -->

<?php

global $post;

if ( post_type_exists( 'featured-products' ) ) {

$testimonial_query = new WP_Query( array(
'post_type' => 'featured-products',
'orderby' => 'rand',
'posts_per_page' => -1
) );

if ( $testimonial_query->have_posts() ) {
$random_int = rand( 0, $testimonial_query->post_count - 1 );
$post = $testimonial_query->posts[$random_int];
setup_postdata( $post );

echo '<div class="find-og-circle-three"><img src="';
echo 'http://dev.oilygurus.com/wp-content/uploads/2015/12/sandi-boudreau.jpg';
//the_field('oil_image');
echo '"></div>';
}
// Restore original post data
wp_reset_postdata();
}

?>

<!-- circle one -->

<?php

global $post;

if ( post_type_exists( 'featured-products' ) ) {

$testimonial_query = new WP_Query( array(
'post_type' => 'featured-products',
'orderby' => 'rand',
'posts_per_page' => -1
) );

if ( $testimonial_query->have_posts() ) {
$random_int = rand( 0, $testimonial_query->post_count - 1 );
$post = $testimonial_query->posts[$random_int];
setup_postdata( $post );

echo '<div class="find-og-circle-one"><img src="';
echo 'http://dev.oilygurus.com/wp-content/uploads/2015/12/sandi-boudreau.jpg';
//the_field('oil_image');
echo '"></div>';
}
// Restore original post data
wp_reset_postdata();
}

?>

Answers (3)

2016-02-23

Hariprasad Vijayan answers:

Hi,

Images are hardcoded?
<blockquote>echo 'http://dev.oilygurus.com/wp-content/uploads/2015/12/sandi-boudreau.jpg';</blockquote>

You need to exclude the posts that already picked, right?

Try this method,
Initiate an array variable top of the section,
$exclude_ids = array();
Add post id to that array after setup_postdata( $post );
$exclude_ids[] = get_the_ID();
Pass $exclude_ids to post__not_in argument to WP_Query of second and third sections
'post__not_in' => $exclude_ids
Complete code would be like this
<!-- circle two -->
<?php
global $post;
$exclude_ids = array();
if ( post_type_exists( 'featured-products' ) ) {
$testimonial_query = new WP_Query( array(
'post_type' => 'featured-products',
'orderby' => 'rand',
'posts_per_page' => -1
) );
if ( $testimonial_query->have_posts() ) {
$random_int = rand( 0, $testimonial_query->post_count - 1 );
$post = $testimonial_query->posts[$random_int];
setup_postdata( $post );
$exclude_ids[] = get_the_ID();
echo '<div class="find-og-circle-two"><img src="';
echo 'http://dev.oilygurus.com/wp-content/uploads/2015/12/sandi-boudreau.jpg';
//the_field('oil_image');
echo '"></div>';
}
// Restore original post data
wp_reset_postdata();
}
?>
<!-- circle three -->
<?php
global $post;
if ( post_type_exists( 'featured-products' ) ) {
$testimonial_query = new WP_Query( array(
'post_type' => 'featured-products',
'orderby' => 'rand',
'posts_per_page' => -1,
'post__not_in' => $exclude_ids
) );
if ( $testimonial_query->have_posts() ) {
$random_int = rand( 0, $testimonial_query->post_count - 1 );
$post = $testimonial_query->posts[$random_int];
setup_postdata( $post );
$exclude_ids[] = get_the_ID();
echo '<div class="find-og-circle-three"><img src="';
echo 'http://dev.oilygurus.com/wp-content/uploads/2015/12/sandi-boudreau.jpg';
//the_field('oil_image');
echo '"></div>';
}
// Restore original post data
wp_reset_postdata();
}
?>
<!-- circle one -->
<?php
global $post;
if ( post_type_exists( 'featured-products' ) ) {
$testimonial_query = new WP_Query( array(
'post_type' => 'featured-products',
'orderby' => 'rand',
'posts_per_page' => -1,
'post__not_in' => $exclude_ids
) );
if ( $testimonial_query->have_posts() ) {
$random_int = rand( 0, $testimonial_query->post_count - 1 );
$post = $testimonial_query->posts[$random_int];
setup_postdata( $post );
echo '<div class="find-og-circle-one"><img src="';
echo 'http://dev.oilygurus.com/wp-content/uploads/2015/12/sandi-boudreau.jpg';
//the_field('oil_image');
echo '"></div>';
}
// Restore original post data
wp_reset_postdata();
}
?>

Hope you are looking for this.

2016-02-23

timDesain Nanang answers:

another approach:
<?php
global $post;
if ( post_type_exists( 'featured-products' ) ) {
$testimonial_query = new WP_Query( array(
'post_type' => 'featured-products',
'orderby' => 'rand',
'posts_per_page' => 3
) );

if ( $testimonial_query->have_posts() ) {
$classes = array(
0 => 'two',
1 => 'three',
2 => 'one',
);
$i = 0;
while($wpq->have_posts()) : $wpq->the_post();
echo '<div class="find-og-circle-'.$classes[$i].'"><img src="';
the_field('oil_image');
echo '" /></div>';

$i++;
endwhile;
}
// Restore original post data
wp_reset_postdata();
}
?>

2016-02-23

Reigel Gallarde answers:

your code can be refactored to this..


if ( post_type_exists( 'featured-products' ) ) {
$testimonial_query = get_posts( array(
'post_type' => 'featured-products',
'orderby' => 'rand',
'posts_per_page' => 3 // we will limit 3 per page to get 3 random products..
));

// arrange class accordingly...
$classes = array('find-og-circle-two','find-og-circle-three','find-og-circle-one');

foreach($testimonial_query as $key => $_product) :
setup_postdata( $_product );
echo '<div class="'.$classes[$key].'"><img src="';
the_field( 'oil_image', $_product->ID );
echo '"></div>';
endforeach;

// Restore original post data
wp_reset_postdata();
}