我目前有以下自定义wp\\u查询,其中显示来自2个自定义wp角色的帖子,“custom\\u role\\u one”和“custom\\u role\\u two”。它显示我的自定义帖子类型“列表”中的帖子。它工作得很好,但我如何才能修改它,使帖子按每个角色排序?
例如,我希望首先显示“custom\\u role\\u one”中的所有帖子,然后列出“custom\\u role\\u two”中的所有帖子。如您所见,它们当前是按日期排序的。
<?php $custom_roles = get_users( array( \'role\' => \'custom_role_one, custom_role_two\' ) );
$custom_ids = array();
foreach( $custom_roles as $custom_role )
$custom_ids[] = $custom_role->ID;
$custom_role_query = new WP_Query( array( \'author\' => implode( \',\', $custom_ids ), \'post_type\' => \'listing\', \'posts_per_page\' => 10, \'orderby\' => \'date\', \'order\' => \'DESC\', \'paged\' => get_query_var(\'paged\') ) );
?>
<?php if ( $custom_role_query ->have_posts() ) : while ( $custom_role_query ->have_posts() ) : $custom_role_query ->the_post(); ?>
<div id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
</div>
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
最合适的回答,由SO网友:Bainternet 整理而成
您可以将自定义字段添加到每个具有用户角色的列表帖子中,然后在查询中可以按该字段排序,例如,假设您有一个名为“u\\u role”的自定义字段,则您的查询应如下所示:
$custom_role_query = new WP_Query( array(
\'author\' => implode( \',\', $custom_ids ),
\'post_type\' => \'listing\',
\'posts_per_page\' => 10,
\'paged\' => get_query_var(\'paged\'),
\'meta_key\' => \'u_role\',
\'orderby\' => \'meta_value\',
\'order\' => \'DESC\', ) );