Wordpress只显示特定作者角色的帖子


Wordpress only show posts from specific author role

我想在循环中包含一个过滤器,这样只有来自具有特定用户角色的作者的帖子才会出现。我想在某个类别中显示它们,例如"已验证的作者",所以我需要在循环中检查这是否也是正确的类别。

这就是我得到的:

functions.php

function get_author_role()
{
global $authordata;
$author_roles = $authordata->roles;
$author_role = array_shift($author_roles);
return $author_role;
 }

类别模板中的循环

<?php
if(have_posts()) : while(have_posts()) : the_post();
//how to check if author = specific role and check if category 'verifiedauthors" ?
endwhile;endif;
?>

您可以将作者查询添加到循环中。

// Example
$atuhors_array = array( 1,4,5,3 );
$query = new WP_Query( array( 'author__in' => $authors_array ) );

一旦你必须创建数组和Add,你就要寻找这个数组的作者。

$blogusers = get_users( 'blog_id=1&orderby=nicename&role=subscriber' );
// Array of WP_User objects.
foreach ( $blogusers as $user ) {
     $authors_array[]= $user->ID;

将作者添加到数组后,现在可以使用作者查询了。