WordPress.org

Ready to get started?Download WordPress

Forums

Not too versatile (9 posts)

  1. illustrationism
    Member
    Posted 5 years ago #

    Are there any plans in the works to make this more useful? For example, having it return an array of workable data instead of a string? Alternatively, is there another function I can use to get all this data in an array?

    I'm guessing there's a plug-in for that functionality, but really it should be a given since having just a list of authors isn't very versatile.

    Thanks,
    David

  2. Samuel B
    moderator
    Posted 5 years ago #

  3. illustrationism
    Member
    Posted 5 years ago #

    I don't know if I'm a bit slow or what, but it looks to me like that page (which I read before) shows how to customize an individual author's page. What I want is to return a list of authors, along with their information, in an array (or lacking that, have a custom page for displaying a list of authors).

    The page you linked to doesn't mention if/how to do that.

    Thanks,
    David

  4. Bas Schuiling
    Member
    Posted 5 years ago #

    And what is it you cannot do with this function?

  5. illustrationism
    Member
    Posted 5 years ago #

    Well, both simple and advanced things.

    Simple: Put my authors list into an HTML table.

    Advanced: Pull author meta data along with username and number of posts and put them into a PHP array.

    I mean, I'm starting to think that I'll have to find or write a plug-in for this. lol

  6. storvin
    Member
    Posted 5 years ago #

    I ran into the same problem as you. It's kind of dumb the only easy function to get the authors in WordPress poops out HTML or simply a comma delimited list of authors without useful further data.

    I had to create this function to get data to make an authors drop-down list for a blog with very special requirements. This just returns the users and takes the args 'exclude_admin', 'hide_empty'. It also adds the number of posts as the 'posts' field, since I needed to look it up anyway to exclude users with zero posts. Sorry if the PHP is wonky in some way, but I haven't done any for 3 years and had to jump into PHP again for this project (not my choice).

    I agree it would've been nice if WordPress had model-only code for getting the authors. They don't really seem to have separated the model data from view. But then, it's a historical problem with PHP not creating boundaries or encouraging programmers to organize their code into mvc.

    /**
         * To get list of authors with posts field added
         * which contains the number of posts for that author
         * boolean arg 'exclude_admin', 'hide_empty'
         */
        function get_authors($args = '') {
            global $wpdb;
    
            $defaults = array(
                'exclude_admin' => true
            );
    
            $r = wp_parse_args( $args, $defaults );
            extract($r, EXTR_SKIP);
            $return = '';
    
            $ex_adm = ($exclude_admin ? "user_login <> 'admin' " : ' 1 = 1');
            $hi_emp = ($hide_empty ? "postnum.posts > 0 " : ' 1 = 1');
    
    $query = <<<EOS
    SELECT $wpdb->users.*,
    COALESCE(postnum.posts,0) as posts FROM $wpdb->users
    LEFT JOIN
    (SELECT post_author, count(post_author) as posts
    FROM $wpdb->posts
    WHERE post_type = 'post'
    GROUP BY post_author) as postnum
    ON $wpdb->users.ID = postnum.post_author
    WHERE $ex_adm AND $hi_emp
    ORDER BY display_name;
    EOS;
    
    		$authors = $wpdb->get_results($query);
            return $authors;
        }
    
    }
  7. storvin
    Member
    Posted 5 years ago #

    Example of usage:

    $args = array('exclude_admin' => false, 'hide_empty' => false);
            $authors = $this->get_authors( $args );

    I also just noticed in my code that I forgot to include a default for 'hide_empty', so it should be the following for defaults instead.

    ' $defaults = array(
    'exclude_admin' => true,
    'hide_empty' => false
    );'

  8. storvin
    Member
    Posted 5 years ago #

    er, I meant.

    $defaults = array(
    'exclude_admin' => true,
    'hide_empty' => false
    );
  9. illustrationism
    Member
    Posted 5 years ago #

    Yeah, that's pretty much what I needed. I already went ahead and wrote it out myself. Code is similar.

    Simple plug-in. Useful, too.

Topic Closed

This topic has been closed to new replies.

About this Topic