• This is a method of ordering your posts by vote count, if you have the ‘Vote it Up’ plugin for WordPress. It’s based on another script I found somewhere else in the forum – but much improved I feel.

    Place this function inside your votingfunctions.php, located at ‘wp-content/plugins/vote-it-up/’, you can pass any “post-filtering” query to it:

    function ShowPostByVotes($query) {
    
    	global $wpdb, $voteiu_databasetable;
    	$upperlimit = get_option('voteiu_limit');
    	if ($upperlimit == '') { $upperlimit = 100; } $lowerlimit = 0;
    
    	$votesarray = array();
    
            $pageposts = $wpdb->get_results($query, OBJECT);
    
    	$query_limited = $query." LIMIT ".$lowerlimit.", ".$upperlimit;
    
    	$posttablecontents = mysql_query( $query_limited );
    
    	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;
    }

    Then create a new loop like this:

    // Example of a query you could use
    $query = "
    	SELECT wposts.*
    	FROM wp_posts wposts, wp_postmeta wpostmeta
    	WHERE wposts.ID = wpostmeta.post_id
    	AND wpostmeta.meta_key = 'yourkey'
    	AND wpostmeta.meta_value = 'yourvalue'
    	AND wposts.post_status = 'publish'
    	AND wposts.post_type = 'yourtype'
    	ORDER BY wposts.post_date DESC
    	";
    
    // This is the loop
    $pageposts = ShowPostByVotes($query);
    if ($pageposts):
    	foreach ($pageposts as $post):
    		setup_postdata($post);
                    // loop content goes here
    		include 'yourloopcontent.php';
    	endforeach;
    endif;

    Hope that helps some people, I know a few were having the same problem as me!

Viewing 10 replies - 1 through 10 (of 10 total)
  • Where do you find “yourkey” “yourvalue” “publish” etc etc…also what file would i put the second line of code in please…

    Thanks for your help.

    nanobyte1111

    (@nanobyte1111)

    Never mind on my last comment, got the votes by using “&orderby=comment_count”

    Does anyone know of an alternative for “comment_count”

    Have tried “votes”, “wp_votes”, etc etc

    am going off of this https://codex.www.ads-software.com/Function_Reference/query_posts#Order_.26_Orderby_Parameters

    This would be awesome if someone could finish the pice im missing! Thanks for the help!

    Thread Starter Vertaxe

    (@vertaxe)

    Hi nanobyte,

    Note that the given $query here is just an example, you can put any query you like.

    You should only need to change the $query, eg.

    $query = "SELECT * FROM wp_posts WHERE post_type = 'post' ORDER BY post_date DESC";

    And the loop content, here is an example:

    <?php
    $post = get_post(get_the_ID());
    
    DisplayVotes($post->ID);
    echo
    '
    '<b>'.$post->post_title.'</b>'.
    $post->post_content;
    
    // etc
    
    ?>

    bdndnet

    (@bdndnet)

    Hi there,

    I’m fairly new to WordPress so things might need explaining here..
    I know HTML and CSS, but PHP is a bit of a stretch currently!

    BTW This plugin is exactly what my client needs, to have the top posts listed on their own page.

    So I’ve put the function in the votingfunctions.php,
    where does the rest of the code go? I am altering the Twenty Ten theme. Do I create a new Template php?

    Any help would be much appreciated!

    Sorry but…what was ‘yourkey’ and ‘yourvalue’ after all?

    Sorry…nevermind my question, figured it out as well. In case anyone has trouble with this I’ll explain a few things you need to change in the given example, and a thanks to vertaxe for sharing.

    1. ‘yourkey’ and ‘yourvalue’ are examples added to the query in case you decide to querya custom field…they are not required to query by your votes. Pretty much everything in the following code can be customized to your own query….which was explained already, but I didn’t really get so am re-sharing.

    $query = "
    	SELECT wposts.*
    	FROM wp_posts wposts, wp_postmeta wpostmeta
    	WHERE wposts.ID = wpostmeta.post_id
    	AND wpostmeta.meta_key = 'yourkey'
    	AND wpostmeta.meta_value = 'yourvalue'
    	AND wposts.post_status = 'publish'
    	AND wposts.post_type = 'yourtype'
    	ORDER BY wposts.post_date DESC
    	";

    2. Watch for the include. Here, you’ll have to include your own loop. Again pretty simple but for some reason I looked over this as well the first time through.

    $pageposts = ShowPostByVotes($query);
    if ($pageposts):
    	foreach ($pageposts as $post):
    		setup_postdata($post);
                    // loop content goes here
    		include 'yourloopcontent.php';
    	endforeach;
    endif;

    I apologize to anyone subscribed to this thread by email for the multiple posts…but the following also needs to be included here. In you are having duplicate posts, you need to add a GROUP BY to your query. For example I am using…

    $query = "
    	SELECT wposts.*
    	FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    	WHERE wposts.ID = wpostmeta.post_id
    	AND wposts.post_status = 'publish'
    	AND wposts.post_type = 'YOUR_POST_TYPE'
    	GROUP BY wposts.ID
    	";

    Adding the GROUP BY causes the loop to automatically exclude any duplicate posts. Hope this helps someone.

    Awesome, thanks Nick, will give this a try…this will definitely make for a much better filter on my front page. Will update results.

    Thanks again.

    Any ideas on getting pagination working?

    Anybody know how to change ShowPostByVotes function to display only 10 posts?

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Vote it Up – Post Order’ is closed to new replies.