Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Vote-it-up plugin. Show posts by votes, but with pagination. WordPress

  • SOLVED

Hello I am in need of help with the plugin Vote-it-up (http://wordpress.org/extend/plugins/vote-it-up/ ) and the code to lists posts by votes. I need to know how to be able to put this into a standard paginated view! (8 posts pr page)

Regular pagination plugins and tags won´t work as it is a custom query :

<?php

$pageposts = ShowPostByVotes();

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

Code is found here : http://wordpress.org/support/topic/plugin-vote-it-up-show-top-voted-post-in-index-page

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;

}

Answers (2)

2010-11-01

Nick Parsons answers:

That function will work great just how it is. Then, on the page that you want to display the post by votes, use this code:

<?php

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$num_posts = get_option('posts_per_page');
$start = $paged*$num_posts -$num_posts;
$end = $paged*$num_posts;
$pageposts = array_slice(ShowPostByVotes(), $start, $end);

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

//Echo title, excerpt, whatever you need here (just like inside of loop)

<?php endforeach; ?>

<?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; ?>


kulan comments:

Great!
Now it is respecting the posts pr page setting and it is sorting itself wonderfully! SO close :) I´m using Wp-page-numbers to show the pages, but that doesn´t seem to pick up the pagination, any thoughts on why that would be?


Nick Parsons comments:

Try replacing that first line with:
global $paged;


Nick Parsons comments:

Excuse me, don't replace the first line, just add it at the beginning like so:


<?php

global $paged;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$num_posts = get_option('posts_per_page');
$start = $paged*$num_posts -$num_posts;
$end = $paged*$num_posts;
$pageposts = array_slice(ShowPostByVotes(), $start, $end);

?>

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

//Echo title, excerpt, whatever you need here (just like inside of loop)

<?php endforeach; ?>
<?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; ?>


kulan comments:

Hm, that does not seem to do the trick, but that might be the plugins fault? Any good suggestions or ideas ?


Nick Parsons comments:

No problem, I'm working on it. I definitely think it's solveable, I just need a bit more time :)


Nick Parsons comments:


Nick Parsons comments:

Actually, it is possible that you could send me the link to the site you're working on?


kulan comments:

Sorry, I can´t do that. But I´ve gotten what I wanted, and that is that it respects the posts pr page, if I get the pagination to work, that would just be a bonus. If you don´t have any other ideas or tricks to try out, I´d still say that I´m very pleased with the answer you´ve provided. Thanks a lot !

The code you sent over works when used without a plugin?

2010-11-01

Maor Barazany answers:

There is an option to use [[LINK href="http://www.ericmmartin.com/pagination-function-for-wordpress/"]]this paginate function[[/LINK]] and combine it with your code.