php - Wordpress - 我如何从我关注的用户那里获取帖子
问题描述
我有这个函数来检索指定用户关注的所有用户。此代码来自插件“用户关注系统”,我只需要从我关注的用户那里获取帖子!
<?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();
}
?>
解决方案
试试这个功能。确保pwuf_get_following()
返回正确的用户 ID。
function pwuf_following_posts_shortcode( $atts, $content = null ) {
// Make sure the current user follows someone
if( empty( pwuf_get_following() ) )
return;
else
$userids = pwuf_get_following();
$userids = array_values(array_filter($userids));
$paged = get_query_var('paged') ? get_query_var('paged') : 1; //remove if pagination works without this
$items = new WP_Query( array(
'post_type' => 'any',
'posts_per_page' => 12,
'order' => 'DESC',
'author__in' => $userids,
'paged' => $paged //remove if pagination works without this
) );
ob_start();
if ( $items->have_posts() ) :
$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;
echo '</div><!-- .posts-wrap -->';
echo paginate_links( array(
'total' => $items->max_num_pages
));
wp_reset_postdata();
else :
get_template_part( 'template-parts/content', 'none' );
endif;
return ob_get_clean();
}
推荐阅读
- sql - 如何从 sql server 中的表中检索以逗号分隔的列形式的单个值?
- sql - 访问:选择下拉列表中的值时执行代码
- php - 我想从自动完成地址表单中获取纬度和经度
- excel - 通过 Excel VBA 将粗体文本导出到 Outlook 邮件
- sql-server - SQL Server 日期数据类型变为 nvarchar
- android - AndroidStudio 中的非法 main-dex-list 条目
- asp.net-core - Aspnetcore:如何发布松散耦合的项目?
- python - 自动优化 Pandas Dtypes
- javascript - 最后以空值排序
- widget - 使用 Drupal hook_form_alter 有条件地更改字段小部件类型