Wordpress loop filtering by posts which have Buddypress author avatars?

时间:2015-07-31 20:33:01

标签: wordpress loops filter buddypress avatar

I have a basic wordpress loop displaying 5 random posts from a custom post type -

$args = array (
    'post_type' => 'home',
    'showposts' => 5,
    'orderby' => 'rand',
            );

Inside the loop I'm displaying the authors profile image as set in Buddypress as follows -

<?php echo get_avatar( $post->post_author, 330 ); ?>

How could I alter my loop to only display posts if the author has set a Buddypress avatar, and ignore it if not?

1 个答案:

答案 0 :(得分:0)

这是WP功能:get_avatar
这是BP功能:bp_core_fetch_avatar

您可以检查用户是否拥有非重力头像,如下所示:

if ( bp_core_fetch_avatar( array( 'item_id' => $post->post_author, 'no_grav' => true, 'html' => false ) ) != bp_core_avatar_default( 'local' ) ) {
            // show the post
}

重新评论 - 这是一个问题。在帖子查询中没有要检查的元数据。你可以:

  • 为每个人创建元 - 一个相当复杂的过程

  • 获取具有头像的所有成员ID并使用author__in WP_Query中的参数 - 如果您有很多,则效率不高 成员

  • 调用更多帖子,以便获得至少5个作者头像