Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9211949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:24:47+00:00 2026-06-18T01:24:47+00:00

I am pulling my hair out here, I simply cannot get this to work.

  • 0

I am pulling my hair out here, I simply cannot get this to work.

I need to do a foreach loop to get all authors in a website, I then need to filter the ones with 0 published articles out and then echo the authors with articles into a UL LI with a special

  • tag for the last author in the array:

    My code at the moment has two functions, one to prefilter all authors that have at least one article and then in the second function count the number of authors left in the filtered array to then give the last entry in the array a special li tag. Code so far:

    /*********************
        Echo Filtered List
        *********************/
        function filtered_list() {
            $authors = get_users('orderby=nicename');
            $all_authors = array();
             if ( count_user_posts( $author->id ) >= 1 ) {
                 return true;
            }
    
        }
    
    
        function contributors() {
    
        $i = 0;
        filtered_list();
        $len = count($all_authors);
        foreach ($all_authors as $author ) {
              if ( count_user_posts( $author->id ) >= 1 ) {
                    if ($i == $len - 1) {
                        echo "<li class='author-last clearfix'>";}
                    else {
                        echo "<li class='author clearfix'>";}
                    $i++;
    
    • 1 1 Answer
    • 0 Views
    • 0 Followers
    • 0
    Share
    • Facebook
    • Report

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Forgot Password?

    Need An Account, Sign Up Here

    1 Answer

    • Voted
    • Oldest
    • Recent
    • Random
    1. Editorial Team
      Editorial Team
      2026-06-18T01:24:49+00:00Added an answer on June 18, 2026 at 1:24 am

      If you read through your code you would probably see why it doesn’t work.

      First: Scopes

      Read about variable scopes in the PHP manual. Basically, a variable declared inside a function is only available inside that function, so $all_authors is null inside contributors() as it has never been initialized.

      The filtered_list function should return a filtered list of authors, so you should loop, though $authors and add the author to $all_authors if, and only if she has 1 or more posts. After the loop, return the array.

      Now you can get the filtered list by setting the return value of the fist function to the $all_authors in contributors (or better yet, just call them $authors).

      Now you are ready to iterate over the list of authors and find their post. To do this, you need two loops. One for authors, and one for the posts.

      foreach author in authors
          foreach post in author->posts
              if post is last post
                  print special stuff
              else
                  print normal stuff
              endif
          endforeach
      endforeach
      

      Hope this helps, and that you’ll learn something from it. Point is: Read though your code line by line and explain to yourself what it does.

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
        • Report

    Sidebar

    Related Questions

    I'm pulling my hair out here, because I just can't seem to get this
    I'm pulling my hair out here trying to get CSS to position only a
    I'm on the verge of pulling my hair out over this. Here I have
    Pulling my hair out here, but I need to convert an int to a
    I am pulling my hair out with this one. I have looked and cannot
    Pulling my hair out with this query. Maybe some of the experts here can
    Pulling my hair out here... I need to insert a bunch of html into
    Pulling my hair out here... have been playing around with this for the last
    I've been pulling my hair out all day on this. Any help will be
    Pulling my hair out over this one. I have one wordpress install at /2009

    Explore

    • Home
    • Add group
    • Groups page
    • Communities
    • Questions
      • New Questions
      • Trending Questions
      • Must read Questions
      • Hot Questions
    • Polls
    • Tags
    • Badges
    • Users
    • Help
    • SEARCH

    Footer

    © 2021 The Archive Base. All Rights Reserved
    With Love by The Archive Base

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.