我认为使用标准WP_Query
您应该运行6个不同的循环,但这是无法承受的。但使用自定义$wpdb
查询你可以做的技巧。。。
function get_unique_posts_tax( $taxonomy = \'category\', $number = 10, $cpt = \'post\') {
global $wpdb;
return $wpdb->get_results( $wpdb->prepare(
"SELECT p.* FROM $wpdb->term_relationships
JOIN $wpdb->term_taxonomy AS tt
ON tt.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id
JOIN $wpdb->posts AS p ON p.ID = $wpdb->term_relationships.object_id
WHERE tt.taxonomy = %s
AND p.post_type = %s AND p.post_status = \'publish\'
GROUP BY $wpdb->term_relationships.term_taxonomy_id
ORDER BY RAND() LIMIT %d", $taxonomy, $cpt, $number
) );
}
将此函数放入
functions.php
之后,你可以
$loop = get_unique_posts_tax( \'author\', 6 );
if ( ! empty($loop) ) {
global $post;
foreach ( $loop as $post ) {
setup_postdata($post);
// Here goes your loop content...
echo \'<p>\' . get_the_title() . \'</p>\';
}
wp_reset_postdata();
}