WordPress获得我关注的用户的帖子

时间:2018-07-28 15:53:33

标签: php wordpress posts author

我有这个代码片段来检索指定用户遵循的所有用户。 我使用的来自插件的这段代码称为“ Users Following System”,对我来说非常有用。

<?php
/**
 * Retrieves all users that the specified user follows
 *
 * Gets all users that $user_id followers
 *
 * @access      private
 * @since       1.0
 * @param   int $user_id - the ID of the user to retrieve following for
 * @return      array
 */

function pwuf_get_following( $user_id = 0 ) {

    if ( empty( $user_id ) ) {
        $user_id = get_current_user_id();
    }

    $following = get_user_meta( $user_id, '_pwuf_following', true );

    if ( empty( $following ) ) {

    return;

    }
    return (array) apply_filters( 'pwuf_get_following', $following, $user_id );
}
?>

现在,我正在尝试带出我关注的用户的所有帖子。 我没有收到任何错误,但它带出了我关注的用户以及其他我从未关注过的用户的帖子,而且分页效果也不佳。

<?php
/**
 * Shows the posts from users that the current user follows
 *
 * @access      private
 * @since       1.0
 * @return      string
 */

function pwuf_following_posts_shortcode( $atts, $content = null ) {

    // Make sure the current user follows someone
    if( empty( pwuf_get_following() ) )
        return;

    $items = new WP_Query( array(
        'post_type'      => 'any',
        'posts_per_page' => 12,
                'order' => 'DESC',
        'author__in' => pwuf_get_following()
    ) );

    ob_start(); ?>
    <?php
        if ( $items->have_posts() ) : ?>

            <?php

            $archive_content_layout = get_option( 'archive_content_layout', 'th-grid-2' );
            echo '<div class="posts-wrap ' . esc_attr( $archive_content_layout ) . '">';

                /* Start the Loop */
                while ( $items->have_posts() ) : $items->the_post();

                    /*
                    * Include the Post-Format-specific template for the content.
                    * If you want to override this in a child theme, then include a file
                    * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                    */
                    get_template_part( 'template-parts/content', get_post_format() );


                endwhile;

                wp_reset_postdata();

            echo '</div><!-- .posts-wrap -->';

            the_posts_pagination();

        else :

            get_template_part( 'template-parts/content', 'none' );

        endif; ?>
<?php 
    return ob_get_clean();

}
?>

请有人可以向我解释问题出在哪里!

0 个答案:

没有答案