WordPress.org

Ready to get started?Download WordPress

Forums

[Plugin: Vote It Up] Show top voted post in index page (14 posts)

  1. sebak_pl
    Member
    Posted 4 years ago #

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

    Tada :). You can build you own digit site ;)

    http://wordpress.org/extend/plugins/vote-it-up/

  2. vilpponen
    Member
    Posted 4 years ago #

    Thanks for the tip sebak_pl!

    I tried this and I got it to work in a funny way. The 7 posts I have on the front page are shown 7 times. So it multiplies the listing for each post.

    Can you tell me where I went wrong?

    Also, if you could tell me how to pull the listings from the last 7 days and 30 days as well, that would be helpful.

    Cheers mate!

  3. cnewland
    Member
    Posted 4 years ago #

    Thank you, sebak_pl! You are awesome! Perfect solution.

    I have it up and working on hotchicksinhijabs.com and didn't have to change hardly anything because your explanation was so complete!

    Cameron

  4. williamle8300
    Member
    Posted 4 years ago #

    sebak_pl, Thanks for the code! that's an awesome solution.

    I have a similar question: is it possible to modify your code so that it will sort the post rankings under a particular tag?

  5. developing8
    Member
    Posted 4 years ago #

    Great mod. How can i set this to only pull posts from category? my query posts doesn't work anymore

  6. nvolpe
    Member
    Posted 4 years ago #

    This is excellent! Thank you so much for this! Can anyone help me modify this function to query posts from today, this week, and this month?

  7. tydende
    Member
    Posted 3 years ago #

    Hi all,

    This is somewhat unrelated, but does anyone know how to alow people to vote over and over again? To remove the prevention of multiple votes from an IP address?

    I've tried changing voteitup.php (using the ticker skin) but I can't seem to keep it so the Vote button remains (rather than showing the Voted image if they already voted).

    I'm just not that familiar editing php.

    If anyone knows or has an idea, it would be greatly appreciated.

    Thanks!

  8. gd42
    Member
    Posted 3 years ago #

    Does this work on other pages?

    I have my index.php showing the posts by date, and I want an other page, where posts are sorted by their score.

    I have a feeling that I need to create a custom template, and insert your code in it to do that. Is that correct?

    An uber-lame question. After I created top.php (and called the default header and other templates in it) all I need to do is somply put a link to the menu pointing to top.php? Or it isn't that simple?

    Anyways, thank you for your awesome work, I was looking for this for a long time.

  9. mattcav
    Member
    Posted 3 years ago #

    wonderful hack...but I have a problem: my vote-it-up posts are in a specific category (IE: you can vote only the posts from the cat: 'reviews') and this sort ALL posts from all categories (including news, and other contests).

    Could you help me to get a specific wpdb query to get results only for a specific category?

  10. mcronin0912
    Member
    Posted 3 years ago #

    Has anybody figured this out yet? I am having the same issue as the first responder - multiplying posts.

    Can somebody mind posting their entire index.php file so I can see how the above code is inserted?

    That would be SO awesome! :)

  11. mattcav
    Member
    Posted 3 years ago #

    I use this hack on a /category page.
    I have deleted the loop:

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

    and changed it with the foreach loop:

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

    It works, but displays all posts from the blog, not only the ones that belongs to the category

  12. mcronin0912
    Member
    Posted 3 years ago #

    This what I have. You will see it publishing each post 3 times

    <?php
    
    function render_content() {
    
    ?>
    
    	<?php if (have_posts()) : while (have_posts()) : the_post(); // the loop ?>
    
    <?php
    
    $pageposts = ShowPostByVotes();
    
    ?>
     <?php if ($pageposts): ?>
      <?php foreach ($pageposts as $post): ?>
        <?php setup_postdata($post); ?>
    
    	<!--Post Wrapper Class-->
    	<div <?php if (function_exists('post_class')) { post_class(); } else { echo 'class="post"'; } ?>>
    
    	<!--Title/Date/Meta-->
    	<div class="title wrap">
    		<div class="post-title">
    			<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
    		</div>
    
    		<div class="post-meta">
    			By
    			<span class="meta-author"><?php the_author_posts_link(); ?></span>
    			 &middot; Comments
    			<span class="meta-comments"><?php comments_popup_link('(0)', '(1)', '(%)'); ?></span>
    		</div>
    		<div class="date">
    			<span class="weekday"><?php the_time( 'l' ); ?><span class="weekday-comma">,</span></span>
    			<span class="month"><?php the_time( 'F' ); ?></span>
    			<span class="day"><?php the_time( 'j' ); ?><span class="day-suffix"><?php the_time( 'S' ); ?></span><span class="day-comma">,</span></span>
    			<span class="year"><?php the_time( 'Y' ); ?></span>
    		</div>
    	</div>
    
    	<!--post text with the read more link-->
    	<div class="post-content">
    		<?php the_content('Read More&rarr;'); ?>
                    <?php DisplayVotes(get_the_ID()); ?>
            </div>
    
    	<!--post meta info-->
    	<div class="meta-bottom wrap">
    		<div class="alignleft"><span class="categories">Categories : <?php the_category(', ') ?></span></div>
    		<div class="alignright"><span class="comments">Comments <?php comments_popup_link('(0)', '(1)', '(%)'); ?></span></div>
    	</div>
    
    	</div><!--end .post-->
    
    <?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; ?>
    
    	<?php endwhile; // end of one post ?>
    	<!-- Previous/Next page navigation -->
    	<div class="paging clearfix">
    		<div class="alignleft"><?php previous_posts_link('&laquo; Previous Page') ?></div>
    		<div class="alignright"><?php next_posts_link('Next Page &raquo;') ?></div>
    	</div>
    
    	<?php else : // do not delete ?>
    
    	<div class="post">
    	<h3><?php _e("Page not Found"); ?></h3>
    	<p><?php _e("We're sorry, but the page you're looking for isn't here."); ?></p>
    	<p><?php _e("Try searching for the page you are looking for or using the navigation in the header or sidebar"); ?></p>
    	</div>
    
    	<?php endif; // do not delete ?>
    <?php
    
    }
    
    add_action( 'builder_layout_engine_render_content', 'render_content' );
    
    do_action( 'builder_layout_engine_render', basename( __FILE__ ) );
    
    ?>

    Any ideas?

  13. mcronin0912
    Member
    Posted 3 years ago #

    @vilpponen, did you figure out how to get rid of the posts multiplying??

  14. Mike Schinkel
    Member
    Posted 3 years ago #

    This example is absolutely AWFUL. It may work but it's far more code than you needs and may break on a future version of WordPress. Use the WordPress API instead, i.e. get_posts() instead of going straight to MySQL.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags