如何从WordPress中的用户ID数组中获取帖子?

时间:2017-08-08 08:14:56

标签: wordpress filter

我有一组用户ID:

$users_id_array = array('1','23','4','7')

我需要显示此阵列的所有帖子。

我尝试了几个代码,但它不起作用。

首先,我使用递归MySQL查询获取一组用户ID,然后在get_posts上应用过滤器。

add_filter( 'get_posts', 'posts_filter_by_id' );
function posts_filter_by_id( $query ) {
    global $wpdb;
    $usuarios_visibles= array();
    // This is a recursive search for get user's id. 
    $busca_usuarios_visibles = $wpdb->get_results( 
        $wpdb->prepare( "        
            SELECT * FROM wpbc_lists_father WHERE id_padre = %d
        UNION SELECT * FROM wpbc_lists_father WHERE id_padre IN 
            (SELECT id_hijo FROM wpbc_lists_father  
            WHERE id_padre = %d)", 
            get_current_user_id(), get_current_user_id()
            ) 
    );
    foreach ($busca_usuarios_visibles as $uv){array_push($usuarios_visibles, $uv->id_hijo);}
    array_push($usuarios_visibles, get_current_user_id());

$args = array(
    'posts_per_page'   => 5,
    'offset'           => 0,
    'category'         => '',
    'category_name'    => '',
    'orderby'          => 'date',
    'order'            => 'DESC',
    'include'          => '',
    'exclude'          => '',
    'meta_key'         => '',
    'meta_value'       => '',
    'post_type'        => 'post',
    'post_mime_type'   => '',
    'post_parent'      => '',
    'author'       => $usuarios_visibles,
    'author_name'      => '',
    'post_status'      => 'publish',
    'suppress_filters' => true 
);
$query = get_posts( $args ); 
    //$query->set( 'posts_per_page', 3 );
    //$query->set( 'author', $busca_usuarios_visibles);
    //$query->set( 'author', get_current_user_id());    
    return $query;
}

2 个答案:

答案 0 :(得分:2)

WordPress已经为此提供了功能。

使用数组作为起点,我们需要将它转换为逗号分隔列表,然后再将其传递给WP_Query对象。

$users_id_array = array('1','23','4','7');

// Convert to comma separated list
$user_ids = implode(',', $users_id_array);

// Get all posts by these authors
$query = new WP_Query( array( 
    'post_type' => 'post',
    'author' => $user_ids 
) );

从这里开始,您可以遍历$query对象以显示帖子。

或者,您可以改为使用author__in(这样就不需要implode()来获取逗号分隔的字符串):

$users_id_array = array('1','23','4','7');

$query = new WP_Query( array( 
    'post_type' => 'post',
    'author__in' => $users_id_array
) );

答案 1 :(得分:0)

现在这项工作,非常感谢! 你的代码简单而优雅!

这是代码:

add_filter( 'pre_get_posts', 'posts_filter_by_id' );
function posts_filter_by_id( $query ) {
/* Comment this real code 
    global $wpdb;
    $usuarios_visibles= array();
    $busca_usuarios_visibles = $wpdb->get_results( 
        $wpdb->prepare( "        
            SELECT * FROM wpbc_lists_father WHERE id_padre = %d
        UNION SELECT * FROM wpbc_lists_father WHERE id_padre IN 
            (SELECT id_hijo FROM wpbc_lists_father  
            WHERE id_padre = %d)", 
            get_current_user_id(), get_current_user_id()
            ) 
    );
    foreach ($busca_usuarios_visibles as $uv){array_push($usuarios_visibles, $uv->id_hijo);}
    array_push($usuarios_visibles, get_current_user_id()); */
    $users_id_array = array('1','23','4','7');
    $user_ids = implode(',', $users_id_array);
    $query->set('author',  $user_ids);
    $query->set('posts_per_page',  5);
    return $query;