Title: Vote function help?
Last modified: August 19, 2016

---

# Vote function help?

 *  [developing8](https://wordpress.org/support/users/developing8/)
 * (@developing8)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/vote-function-help/)
 * How can change this function to only pull posts from a category? say category“
   7” and only 10 posts:
 * **Function called in top 10 posts page**
 * >  <?php $pageposts = ShowPostByVotes(); ?>
   >  <?php if ($pageposts): ?> <?php 
   > foreach ($pageposts as $post): ?> <?php setup_postdata($post); ?> print content
   > etc….. <?php endforeach; ?>
   >  <?php else : ?>
   >  no posts exist please etc……. <?php endif; ?>
 * **In the plugin functions file:**
 * > //Sort posts by vote amount
   >  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;
   > }

The topic ‘Vote function help?’ is closed to new replies.

## Tags

 * [category](https://wordpress.org/support/topic-tag/category/)
 * [display](https://wordpress.org/support/topic-tag/display/)
 * [function](https://wordpress.org/support/topic-tag/function/)
 * [order](https://wordpress.org/support/topic-tag/order/)
 * [vote](https://wordpress.org/support/topic-tag/vote/)

 * 0 replies
 * 1 participant
 * Last reply from: [developing8](https://wordpress.org/support/users/developing8/)
 * Last activity: [15 years, 11 months ago](https://wordpress.org/support/topic/vote-function-help/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
