您最好的解决方案是运行single 查询要查询的位置
所有帖子类型
无论分类如何,所有帖子都来自
一旦您拥有了所有这些,就可以使用usort()
对结果进行相应排序。我不知道您希望您的帖子类型如何排序,但假设您希望按帖子类型和期限排序
好的,让我们看一下代码,但在开始之前,让我们看一些重要的注释
需要考虑的重要注意事项以下代码未经测试,因此可能存在错误。建议在启用调试的情况下首先在本地运行此操作
该代码至少需要PHP5。4,并且由于短数组语法和直接数组解引用,在旧版本上会导致致命错误。如果您仍在使用旧版本,建议您升级。PHP5之前的所有版本。4已经下线,如果您仍在使用这些版本,则会给您的站点带来巨大的安全风险
VERY VERY IMPORTANT: 中的排序功能usort()
只有在查询中有一个或两个帖子类型时,才会按预期工作;如果有两个以上的帖子类型,则函数中的逻辑将无法正常工作。此外,如果每个帖子类型都分配了一个分类法,那么逻辑也会像预期的那样工作。此外,如果任何职位分配了多个术语,则只有第一个术语用于排序我将在编写代码时解释代码,以便于理解
代码
// Define our query arguments
$args = [
\'post_type\' => [\'dramas\', \'reality_shows\'], // Get posts from these two post types
// Define any additional arguments here, but do not add taxonomy parameters
];
$q = new WP_Query( $args );
/**
* We now need to sort the returned array of posts stored in $q->posts, we will use usort()
*
* There is a bug in usort causing the following error:
* usort(): Array was modified by the user comparison function
* @see https://bugs.php.net/bug.php?id=50688
* This bug has yet to be fixed, when, no one knows. The only workaround is to suppress the error reporting
* by using the @ sign before usort
*
* UPDATE FROM COMMENTS FROM @ birgire
* The usort bug has been fixed in PHP 7, yeah!!
*/
@usort( $q->posts, function ( $a, $b )
{
// Sort by post type if two posts being compared does not share the same post type
if ( $a->post_type != $b->post_type )
return strcasecmp( $a->post_type, $b->post_type ); // Swop the two variable around for desc sorting
/**
* I we reach this point, it means the two posts being compared has the same post type
* We will now sort by taxonomy terms
* The logic for the code directly below is, we have two post types, and a single post can only be assigned to
* one post type, so a post can only be from the dramas post type or from the reality_shows post type. We also just
* need to check one of the two posts being compared as we know both posts are from the same post type. If they
* they were not, we would not have been here
*
* The socond part of the logic is that the drama_taxonomy is only assigned to the dramas post type and the
* reality_shows_taxonomy is only assigned to the reality_shows post type, so we can set the taxonomy according
* to post type
*/
$taxonomy = ( $a->post_type == \'dramas\' ) ? \'drama_taxonomy\' : \'reality_shows_taxonomy\';
$terms_a = get_the_terms( $a->ID, $taxonomy );
$array_a = ( $terms_a && !is_wp_error( $terms_a ) ) ? $terms_a[0]->name : \'zzzzz\'; // zzzz is some crappy fallback
$terms_b = get_the_terms( $b->ID, $taxonomy );
$array_b = ( $terms_b && !is_wp_error( $terms_b ) ) ? $terms_b[0]->name : \'zzzzz\'; // zzzz is some crappy fallback
// We will now sort by terms, if the terms are the same between two posts being compared, sort by date
if ( $array_a != $array_b ) {
return strcasecmp( $array_a, $array_b ); // Swop the two variables around to swop sorting order
} else {
return $a->post_date < $b->post_date; // Change < to > to swop sorting order around
}
}, 10, 2 );
// $q->posts is now reordered and sorted by post type and by term, simply run our loop now
你的循环应该是这样的。您需要对此进行修改以满足您的确切需要if ( $q->have_posts() ) {
// Define variable to hold previous post term name
$term_string = \'\';
while ( $q->have_posts() ) {
$q->the_post();
// Set the taxonomy according to post type
$taxonomy = ( \'reality_shows\' == get_post_type() ) ? \'reality_shows_taxonomy\' : \'drama_taxonomy\';
// Get the post terms. Use the first term\'s name
$terms = get_the_terms( get_the_ID, $taxonomy );
$term_name = ( $terms && !is_wp_error( $terms ) ) ? $terms[0]->name : \'Not assigned\';
// Display the taxonomy name if previous and current post term name don\'t match
if ( $term_string != $term_name )
echo \'<h2>\' . $term_name . \'</h2>\'; // Add styling and tags to suite your needs
// Update the $term_string variable
$term_string = $term_name;
// REST OF YOUR LOOP
}
wp_reset_postdata();
}