Function from WordPress VoteItUp function This code comes from a Wordpress plugin's forum called Vote it Up.
It sort posts by vote. A lotof people say that there is more code than is needed.
So I was wondering if someone have any idea about how to clean it a bit.

votingfunctions.php:

function ShowPostByVotes() {
    global $wpdb, $voteiu_databasetable;

    mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die(mysql_error());
    mysql_select_db(DB_NAME) or die(mysql_error());
    //Set a limit to reduce time taken for script to run
    $upperlimit = get_option('voteiu_limit');
    if ($upperlimit == '') {
        $upperlimit = 100;
    }
    $lowerlimit = 0;

    $votesarray = array();
        $querystr = "
            SELECT *
            FROM $wpdb-&gt;posts
            WHERE post_status = 'publish'
            AND post_type = 'post'
            ORDER BY post_date DESC
         ";
        $pageposts = $wpdb-&gt;get_results($querystr, OBJECT);
    //Use wordpress posts table
    //For posts to be available for vote editing, they must be published posts.
    mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die(mysql_error());
    mysql_select_db(DB_NAME) or die(mysql_error());
    //Sorts by date instead of ID for more accurate representation
    $posttablecontents = mysql_query("SELECT ID FROM ".$wpdb-&gt;prefix."posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT ".$lowerlimit.", ".$upperlimit."") or die(mysql_error());

    $returnarray = array();
    while ($row = mysql_fetch_array($posttablecontents)) {
        $post_id = $row['ID'];
        $vote_array = GetVotes($post_id, "array");
        array_push($votesarray, array(GetVotes($post_id)));
    }
    array_multisort($votesarray, SORT_DESC, $pageposts);
    $output = $pageposts;
    return $output;

}


index.php:



$pageposts = ShowPostByVotes();

?&gt;
 &lt;?php if ($pageposts): ?&gt;
  &lt;?php foreach ($pageposts as $post): ?&gt;
    &lt;?php setup_postdata($post); ?&gt;


Attention! Code above is something like:

&lt;?php if (have_posts()) : ?&gt;

        &lt;?php while (have_posts()) : the_post(); ?&gt;


so in foreach loop you can use statements like in standard "The Loop" for example the_content, the_time().
To end this add

&lt;?php endforeach; ?&gt;

  &lt;?php else : ?&gt;
    &lt;h2 class="center"&gt;Not Found&lt;/h2&gt;
    &lt;p class="center"&gt;Sorry, but you are looking for something that isn't here.&lt;/p&gt;
    &lt;?php include (TEMPLATEPATH . "/searchform.php"); ?&gt;
 &lt;?php endif; ?&gt;


EDIT:

How I run a custom loop:

        &lt;?php $custom_posts = new WP_Query(); ?&gt;
        &lt;?php $custom_posts-&gt;query('category_name=Pictures'); ?&gt;
        &lt;?php while ($custom_posts-&gt;have_posts()) : $custom_posts-&gt;the_post(); ?&gt;
            &lt;div class="content-block-2"&gt;
                &lt;a href="&lt;?php the_permalink(); ?&gt;" title="&lt;?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?&gt;" rel="bookmark"&gt;&lt;?php the_content(); ?&gt;&lt;/a&gt;
            &lt;/div&gt;
        &lt;?php endwhile; ?&gt;
These are my suggestions


Don't use mysql functions to connect and query Wordpress posts. 
You can replace the first SQL statement with this WP_Query. 

$query = new WP_Query('post_type=post&amp;post_status=publish&amp;orderby=date&amp;order=DESC');
$pageposts = $query-&gt;get_posts();  


This way the code is somewhat protected from future Wordpress database changes.
The second select statement is a bit unnecessary as each $post in the $pagepost already contains the post id. 
Only thing missing is the limit, but we'll add that to the WP_Query by adding

$query = new WP_Query('post_type=post&amp;post_status=publish&amp;orderby=date&amp;posts_per_page='.$upperlimit);

Refactor and remove unused/unnecessary code

global $wpdb, $voteiu_databasetable - are not needed anymore
$upperlimit - assignment can be done on one line
$lowerlimit - not used anymore as it's enough with the $upperlimit 
$output = $pageposts; - assigmentent before return is unnecessary


So the complete ShowPostByVotes in votingfunctions.php would look something like this now:

function ShowPostByVotes() {

    $upperlimit = is_numeric(get_option('voteiu_limit')) ? get_option('voteiu_limit') : 100 ;

    $query = new WP_Query('post_type=post&amp;post_status=publish&amp;orderby=date&amp;posts_per_page='.$upperlimit);
    $pageposts = $query-&gt;get_posts();        
    $votesarray = array();
    foreach ($pageposts as $post) {
        $vote_array = GetVotes($post-&gt;ID, "array");
        array_push($votesarray, array(GetVotes($post-&gt;ID)));
    }

    array_multisort($votesarray, SORT_DESC, $pageposts);
    return $pageposts;

}


And you can use it in your index.php by using this code

 &lt;?php $pageposts = ShowPostByVotes();  ?&gt;
 &lt;?php if ($pageposts): ?&gt;
  &lt;?php global $post; ?&gt;
  &lt;?php foreach ($pageposts as $post): ?&gt;
    &lt;?php setup_postdata($post); ?&gt;

    &lt;div class="post" id="post-&lt;?php the_ID(); ?&gt;"&gt;
      &lt;h2&gt;&lt;a href="&lt;?php the_permalink() ?&gt;" rel="bookmark" title="Permanent Link to &lt;?php the_title(); ?&gt;"&gt;
      &lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/h2&gt;
      &lt;small&gt;&lt;?php the_time('F jS, Y') ?&gt; &lt;!-- by &lt;?php the_author() ?&gt; --&gt;&lt;/small&gt;
      &lt;div class="entry"&gt;
         &lt;?php the_content('Read the rest of this entry »'); ?&gt;
      &lt;/div&gt;

      &lt;p class="postmetadata"&gt;Posted in &lt;?php the_category(', ') ?&gt; | &lt;?php edit_post_link('Edit', '', ' | '); ?&gt;  
      &lt;?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?&gt;&lt;/p&gt;
    &lt;/div&gt;
  &lt;?php endforeach; ?&gt;

  &lt;?php else : ?&gt;
    &lt;h2 class="center"&gt;Not Found&lt;/h2&gt;
    &lt;p class="center"&gt;Sorry, but you are looking for something that isn't here.&lt;/p&gt;
    &lt;?php include (TEMPLATEPATH . "/searchform.php"); ?&gt;
 &lt;?php endif; ?&gt;