我认为这应该是可行的,但我对“not”空字符串的高级元查询不是百分之百确定。这通常不是元查询的使用方式。因此,我离开了set_transient
行已注释掉。我刚刚注意到您正在尝试随机抽取1个帖子,因此您可能不想使用Transients API 虽然如此,但我认为缩短时间限制仍然是一个好主意,因此我将瞬态设置为存储1小时。如果没有,则始终可以提取查询部分。
// 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();
关于的优秀参考
Advanced Meta Queries