I think this should work, but I am not 100% certain on the advanced meta query for “not” a null string. That isn’t normally how meta queries are used. As such, I have left the set_transient
line commented out. I just noticed that you are trying to pull 1 random post, so you might not want to use the Transients API at all, but I think it would still be a good idea just with a shorter time limit, so I have the transient set to store for 1 hour. If not, you can always extract the query parts.
// Get any existing copy of our transient data
if ( false === ( $custom_testimonials = get_transient( 'custom_testimonials' ) ) ) {
// It wasn't there, so regenerate the data and save the transient
// params for our query
array(); ?
$args = array(
'post_type' => 'our-clients-list'
'posts_per_page' => 1,
'orderby' => 'rand'
'meta_key' => '_featured',
'meta_value' => 'yes',
'meta_query' => array(
array(
'key' => 'testimonial_',
'value' => '',
'compare' => '!='
)
)
);
// The Query
$custom_testimonials = new WP_Query( $args );
// store the transient - uncomment when sure the query is working (stores for 1 hour)
// set_transient( 'custom_testimonials', $custom_testimonials, 60*60*1 );
}
// Use the data like you would have normally...
// The Loop
if ( $custom_testimonials ) :
echo '<ul class="testimonial">';
while ( $custom_testimonials->have_posts() ) :
$custom_testimonials->the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
echo '</ul>';
else :
echo 'No testimonials found.';
endif;
/* Restore original Post Data
* NB: Because we are using new WP_Query we aren't stomping on the
* original $wp_query and it does not need to be reset.
*/
wp_reset_postdata();
Excellent reference on Advanced Meta Queries