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.

The following is a tutorial to Sort Posts by Vote (for the Vote it Up plugin):

I haven't found it anywhere so i put it here. This is a new function which add posibilities to sort post on index page by vote.

Add this in 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->posts
            WHERE post_status = 'publish'
            AND post_type = 'post'
            ORDER BY post_date DESC
         ";
        $pageposts = $wpdb->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->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;

}

and in your index.php page add:

<?php

$pageposts = ShowPostByVotes();

?>
 <?php if ($pageposts): ?>
  <?php foreach ($pageposts as $post): ?>
    <?php setup_postdata($post); ?>

Attention! Code above is something like:

<?php if (have_posts()) : ?>

        <?php while (have_posts()) : the_post(); ?>

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

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

The guy who wrote the code went directly to the MySQL, so this code may break in future Worpdress releases.

How to make this code to follow better Wordpress practices?

EDIT:

GetVotes function:

//Returns the vote count
function GetVotes($post_ID, $percent = false) {
    global $wpdb;

    //prevents SQL injection
    $p_ID = $wpdb->escape($post_ID);

    //Create entries if not existant
    SetPost($p_ID);

    //Gets the votes
    $votes_raw = $wpdb->get_var("SELECT votes FROM  ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
    $sinks_raw = $wpdb->get_var("SELECT usersinks FROM  ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
    $guestvotes_raw = $wpdb->get_var("SELECT guests FROM  ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
    $guestsinks_raw = $wpdb->get_var("SELECT guestsinks FROM  ".$wpdb->prefix."votes WHERE post='".$p_ID."'");
/* Deprecated
    $uservotes_raw = $wpdb->get_var("SELECT votes FROM ".$wpdb->prefix."votes_users WHERE user='".$u_ID."'");
    $usersinks_raw = $wpdb->get_var("SELECT sinks FROM ".$wpdb->prefix."votes_users WHERE user='".$u_ID."'");
*/

    //Put it in array form
    $votes = explode(",", $votes_raw);
    $sinks = explode(",", $sinks_raw);
    $guestvotes = explode(",", $guestvotes_raw);
    $guestsinks = explode(",", $guestsinks_raw);
/* Deprecated
    $uservotes = explode(",", $uservotes_raw);
    $usersinks = explode(",", $usersinks_raw);
*/
    $uservotes = 0;
    $usersinks = 0;

    $initial = 0; //Initial no. of votes [will be placed at -1 when all posts receive votes]



    //The mathematics
    if ($percent == true) {
        // make $votecount into a percent
        $totalcount = count($votes) + count($sinks) + count($guestvotes) + count($guestsinks) + count($uservotes) + count($usersinks) + get_option('voteiu_initialoffset') - 6;
        // the -6 is because count('') returns 1, so if there is no votes at all, 1 is returned.  -6 offsets that
        $forcount = count($votes) + count($guestvotes) + count($uservotes) + get_option('voteiu_initialoffset') - 3;
        $againstcount = count($sinks) + count($guestsinks) + count($usersinks) - 3;
        if ($totalcount > 0) {
            $votecount = number_format(100*($forcount / $totalcount), 0) . "%";
        } else {
            return false;
        }
        return $votecount;
        // uncomment this line below if you want to test
        //return count($votes) . " " . count($sinks) . " " . count($guestvotes) . " " . count($guestsinks) . " " . count($uservotes) . " " . count($usersinks) . " " . get_option('voteiu_initialoffset') . " " . $p_ID;
    } else {
        // wihtout percent mode, $votecount is number of total positive votes (votes minus sinks)
        $votecount = count($votes) - count($sinks) + count($guestvotes) - count($guestsinks) + count($uservotes) - count($usersinks) + $initial;
        return $votecount + get_option('voteiu_initialoffset');
    }
}

function GetPostVotes($post_ID) {
    global $wpdb;

    //prevents SQL injection
    $p_ID = $wpdb->escape($post_ID);

    //Create entries if not existant
    SetPost($p_ID);

    //Gets the votes
    $votes_raw = $wpdb->get_var("SELECT votes FROM  ".$wpdb->prefix."votes WHERE post='".$p_ID."'");

    //Put it in array form
    $votes = explode(",", $votes_raw);
    return $votes + get_option('voteiu_initialoffset');
}
share|improve this question
    
if you can post the code of GetVotes function that would help –  Bainternet Mar 10 '11 at 13:27
    
@Bainternet OK I included the code of the GetVotes function, please see my EDIT. (I know a lot of people are looking for this answer). –  janoChen Mar 10 '11 at 15:54
    
hard to tell since that function call it's own database tables so i guess the only way is by sql queries –  Bainternet Mar 10 '11 at 18:36
add comment

1 Answer

up vote 1 down vote accepted

I can't figure out how to setup this code. I've been dabbling with PHP for a while, but I'm only recently trying to go deeper into how all the WordPress stuff works.

I don't understand what "Attention! Code above is something like:" means. Does that mean we put the previous code below it?

Would you be able to do a quick newbie explanation of all the steps? That would be highly appreciated! Also, is there a way to make it so instead of showing the top posts from the entire site, it shows the top posts in each category?

Thanks again@

share|improve this answer
add comment

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.