WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need array only all author ids from the loop, each one only once if possible and out of all posts in the loop. Any suggestions?

share|improve this question
Can you please share some code in order to understand better your situation and what you want. Also if possible please provide an example. – Ofir Baruch Aug 13 '13 at 11:13
Please don't cross post on two network sites. And if you're doing it with a non network site (like wp.org forums) then please leave a link on both ends. – kaiser Aug 13 '13 at 15:31
up vote 2 down vote accepted

Assuming that you have an array of post objects in $my_posts...

$authids = array_unique(wp_list_pluck($my_posts,'post_author'));

What you will get are the post authors for the current page of posts, not the post authors for all of the posts. If you want the authors for all of the posts you will have run another query.

To run a new query based on the main query in $wp_query...

$this_query = array_unique($wp_query->query_vars);
$this_query['posts_per_page'] = -1;
$new_query = new WP_Query($this_query);

$authids = array_filter(wp_list_pluck($new_query->posts,'post_author'));
var_dump($authids); // debug

I caution you against this. You could be querying hundreds or thousands of posts. That could be very inefficient or even cause server failures, depending on the server and the load. I am almost certain that there is a better way to do this if you think it through and alter your architecture.



share|improve this answer
but i need all of them its important, can you give me the code for the second query and array please? – Eddy Unruh Aug 13 '13 at 14:19
that works perfectly thanks. i think my server can handle it, it has the best components. and i cant imagine anything better to filter the posts (in my case products for price comparsion) besides multiserver, what would be your suggestion? im gona use caching. would an normal php query need much less performance to get and sort the data than the wp query? – Eddy Unruh Aug 13 '13 at 15:05
oh and one more thing, i need to show each author id only one time, it worked with this code: if(have_posts()){ $author_arr = array(); while(have_posts()){ the_post(); global $post; if(!in_array($post->post_author,$author_arr)){ $author_arr[] = $post->post_author; } } } but it works only on current page, not all posts – Eddy Unruh Aug 13 '13 at 15:10
Sorry. Use array_unique instead of array_filter – s_ha_dum Aug 13 '13 at 16:15
thanks very much now its perfect :) – Eddy Unruh Aug 13 '13 at 16:20

Normally there's the WP_User_Query to fetch users (/authors). But as I read that you want to have all authors from the current page loop-posts, it might be easier to hook into the loop it self and collect it.

namespace WPSE109876;

/** Plugin Name: WPSE (#109876) Collect Author IDs */
defined( 'ABSPATH' ) OR exit;

\add_action( 'loop_start', array( __NAMESPACE__.'\LoopCollector', 'init' ) )
class LoopCollector
    public static $instance = null;

    public $stack = array();

    public function init()
        is_null( self::$instance ) AND self::$instance = new self;
        return self::$instance;

    public function __construct()
        \add_action( 'the_post', array( $this, 'toStack' ) );
        \add_action( 'shutdown', array( $this, 'printStack' ) );

    public function toStack( $post )
        $aid = absint( $post->post_author );
        ! in_array( $aid, $this->stack )
            $this->stack[] = $aid;

    public function printStack()
        printf( '<hr />Author IDs<pre>%s</pre>', join( "</br>", $this->stack ) );
share|improve this answer
no i dont want the author ids only from current page but from all, the code from s_ha_dum is almost what i need, the only thing is he fetch every author id even if its multiple. so what i need is getting all author ids but when the author id is for example "2" on one or more posts, then the "2" should appear only one time in array – Eddy Unruh Aug 13 '13 at 15:55
Ehm... "no"? Take a look at the exact first line of the answer. – kaiser Aug 13 '13 at 15:56
nvm s_ha_dum solved it, thanks anyway – Eddy Unruh Aug 13 '13 at 16:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.