Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i have a page who display the latest posts of an author. But when the author has no post i get a display message

Warning: rsort() expects parameter 1 to be array, null given in ...

Here the script

function wpmu_latest_post_auther($authorId,$how_many = 10, $how_many_words = 50, $more_text = "[...]", $remove_html = true, $sort_by = 'post_date') {
global $wpdb;

//first, gat all blog id
$query = "SELECT blog_id FROM $wpdb->blogs WHERE blog_id !='1'";
$blogs = $wpdb->get_col($query);

if ($blogs) {
    //we use blog id to loop post query
    foreach ($blogs as $blog) {
        $blogPostsTable = 'wp_'.$blog.'_posts';

         $db_query = "SELECT $blogPostsTable.ID,
                    $blogPostsTable.post_author,
                    $blogPostsTable.post_title,
                    $blogPostsTable.guid,
                    $blogPostsTable.post_date,
                    $blogPostsTable.post_content,
                    $blogPostsTable.post_modified,
                    $blogPostsTable.comment_count
                    FROM $blogPostsTable WHERE $blogPostsTable.post_status = 'publish'
                    AND $blogPostsTable.post_author = $authorId 
                    AND $blogPostsTable.post_type = 'post'";

        $thispos = $wpdb->get_results($db_query);

        foreach($thispos as $thispost) {
        if($sort_by == 'post_date') {
            $order = $thispost->post_date;
        }
        else{
            $order = $thispost->post_modified;
            }

            $post_dates[]           = $order;
            $post_guids[$order]     = $thispost->guid;
            $blog_IDs[$order]       = $blog;
            $post_IDs[$order]       = $thispost->ID;
            $post_titles[$order]    = $thispost->post_title;
            $post_authors[$order]   = $thispost->post_author;
            $post_contents[$order]  = $thispost->post_content;
            $comments[$order]       = $thispost->comment_count;
            }
        }


    rsort($post_dates);
    $union_results  = array_unique($post_dates);
    $ResultArray    = array_slice($union_results, 0, $how_many);

    foreach ($ResultArray as $date) {
        $ID                 = $post_IDs[$date];
        $blogID             = $blog_IDs[$date];
        $id_author          = $post_authors[$date];
        $post_url           = get_blog_permalink($blog_IDs[$date], $ID);/*$post_guids[$date];*/
        $post_title         = $post_titles[$date];
        $post_content       = $post_contents[$date];
        $post_date          = mysql2date(get_option('date_format'), $date);
        $post_time          = mysql2date(get_option('time_format'), $date);
        $total_comment      = $comments[$date];
        $user_info          = get_userdata($id_author);
        $author_blog_url    = get_blogaddress_by_id($user_info->primary_blog);
        $author_url         = $user_info->user_url;
        $author_email       = $user_info->user_email;

        if($user_info->first_name) {
            $author_name = $user_info->first_name.' '.$user_info->last_name;
        }
        else{
            $author_name = $user_info->nickname;
        }

        if($remove_html) {
            $post_content = wpmu_cleanup_post($post_content);
        }

        $results = array();

        $results['ID']              = $ID;
        $results['blogID']          = $blogID;
        $results['post_url']        = $post_url;
        $results['post_title']      = $post_title;
        $results['post_content']    = wpmu_cut_article_by_words($post_content, $how_many_words);
        if ($results['post_content'] != $post_content)
            $results['post_content'] .= sprintf('  <a href="%s">%s</a>', $post_url, $more_text);
        $results['author_blog_url'] = $author_blog_url;
        $results['author_url']      = $author_url;
        $results['author_email']    = $author_email;
        $results['author_name']     = $author_name;
        $results['post_date']       = $post_date;
        $results['post_time']       = $post_time;
        $results['comment_count']   = $total_comment;

        $returns[] = $results;
    }

    $latest_posts = wpmu_bind_array_to_object($returns);
    return $latest_posts;
} }

Is someone has an idea on how to not display the warning when the author as no post added.

Regards

share|improve this question

2 Answers 2

up vote 0 down vote accepted

Try wrapping the rsort bit in an if statement like this:

if (!empty($post_dates)) {
    rsort($post_dates);
} else { echo 'optional error message here'; }

This will check to see if the variable is empty first, and only run rsort() if it is not empty. This check works most of the time, but a false positive can sometimes occur if, for example, the variable is not actually an array but contains other data! (such as an error message). Alternatively, you may find this to work better in certain circumstances:

if ($post_dates != '') {
    rsort($post_dates);
}

This will only continue if the variable does not equal null, which isn't quite the same thing as being empty. There are other ways to check if the variable is what you want, for example if (is_array($post_dates)) { do stuff }.

share|improve this answer
    
Not sure if the rest of your page will continue to work without that $post_dates filled. Might need to do something "else" with your else. –  GhostToast Jun 1 '12 at 19:40
    
Thanks, this help me to solve my problem. –  neomale Jun 4 '12 at 14:03

Since you are grabbing the blog_id early in the piece what you can do is check to see whether the particular blog in question has any posts and if it does, proceed.

One way to do this is by using the get_blog_details() function.

$blog = get_blog_details( $blod_id );
$total_posts = $blog->post_count;

if ($total_posts == O) {

// do nothing (eg. echo 'this author has no posts';) 

} else {

// your regular code here

}

By feeding get_blog_details the blog ID number, you gain access to a whole range of properties, one of which being post_count for which you can use within the above if/else conditional statement to run a check before prior to executing your function.

Thus you can now display a different result set/piece of information in instances where a user has no posts and where they do have posts your regular code will execute as per normal.

Alternatively the count_user_posts() function could be used, however the above should suffice in this instance.

For reference purposes you can find more information at;

Hope that helps you.

share|improve this answer

Your Answer

 
discard

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.