Forums

[resolved] Help with code to exclude certain authors from author list (3 posts)

  1. sjcallas
    Member
    Posted 10 months ago #

    Hello,

    I don't know much PHP and I've done a google search and a forum search for a solution, but have had no luck. My theme's code that needs editing is:

    <?php
                $authors = $wpdb->get_results('SELECT DISTINCT post_author FROM '.$wpdb->posts);
                if($authors):
                foreach($authors as $author):
                ?>

    How do I alter this code so that I can exclude multiple authors for the listing (say I want to exclude 'admin' , 'user1' , and 'user2' ). Thank you in advance

  2. vtxyzzy
    Member
    Posted 10 months ago #

    You can create an array of author ids to exclude and then check for them in the foreach loop:

    <?php
                $authors = $wpdb->get_results('SELECT DISTINCT post_author FROM '.$wpdb->posts);
                if($authors):
                   $excludes = array(1,14,53);
                   foreach($authors as $author):
                      if (in_array($author->post_author,$excludes)) continue;
                ?>
  3. sjcallas
    Member
    Posted 10 months ago #

    Thanks, that worked like a charm!

Reply

You must log in to post.

About this Topic