User voted query and most voted query?

I would like to make two post voting queries.

1. I would like too see the finished query for this: Order by Most to Least Votes?

2. A query that I can use to display the user voted content only.

Here is what I have tried: wdpv_query_within($timespan, $limit=5, $query=array('orderby' => get_popular_on_current_site(), 'user_id' => get_current_user_id()));
Both the args are just meant to show what I am trying to achieve
I also know that votes aren't put into post meta

I would still like to be able to a time span if possible.

I've been looking at all the classes and such trying to figure this out. I would also like to use a query so I can control the layout a lot better.

Thank you for any support I might get.

  • Vinod Dalvi

    Hi @Corbin,

    Thank you for your question.

    Just to update you about the thing that the Post Voting Plugin contains wdpv_popular shortcode that displays a list of posts with highest number of votes. you will find more information about it on the following page.

    https://premium.wpmudev.org/project/post-voting-plugin/#usage

    1. I would like too see the finished query for this: Order by Most to Least Votes?

    2. A query that I can use to display the user voted content only.

    You can create a query for this using the already developed queries in the functions get_popular_on_network and get_popular_on_site in the following Post Voting Plugin file.

    \wpmu-dev-post-votes\lib\class_wdpv_model.php

    Please advise if you have more questions.

    Kind Regards,
    Vinod Dalvi

  • Corbin

    Thank you very much!

    I'm kind of torn between what I should do. Should I edit the shortcodes/timeframe template tags to have all the functions/template tags in it that I want?

    LIke: Pagination, the post thumbnail, post meta and so on. Or use and add to the queries from the functions and create a foreach loop in my templates. What would be your suggestion?

    Is this the queries I'm suppose to be looking for? NoteI'm fresh to sql, but will learn what I need to quick if need be.

    get_popular_on_site()

    // Woot, mega complex SQL
    		$sql = "SELECT *, SUM(vote) as total FROM " . // SUM(vote) for getting the count - GROUP BY post_id to get to individual posts
    				$this->db->base_prefix . "wdpv_post_votes LEFT JOIN " . // Get the post data too - LEFT JOIN what we need
    				$this->db->prefix . "posts ON " . $this->db->prefix . "posts.ID=" .	$this->db->base_prefix . "wdpv_post_votes.post_id " .
    				"WHERE site_id={$site_id} AND blog_id={$blog_id} AND post_status='publish' " . // Only posts on set site/blog
    				(
    					$posted_timeframe ?
    						"AND post_date > '{$start_date}' AND post_date < '{$end_date}' "
    						: ''
    				) .
    				(
    					$voted_timeframe ?
    						"AND date > '{$voted_start_date}' AND date < '{$voted_end_date}' "
    						: ''
    				) .
    				"GROUP BY post_id " . // Group by post_id so we get the proper vote sum in <code>total</code>
    				"ORDER BY total DESC " . // Order them nicely
    			"LIMIT {$limit}";

    get_popular_on_network()

    $sql = "SELECT *, SUM(vote) as total FROM " . // SUM(vote) for getting the count
    			$this->db->base_prefix . "wdpv_post_votes " .
    			"WHERE site_id={$site_id} AND blog_id<>0 " . // Only posts on multisite sites/blogs
    			(
    				$voted_timeframe ?
    					"AND date > '{$voted_start_date}' AND date < '{$voted_end_date}' "
    					: ''
    			) .
    			"GROUP BY post_id, site_id, blog_id " . // Group by post_id so we get the proper vote sum in <code>total</code>
    			"ORDER BY total DESC " . // Order them nicely
    		"LIMIT {$limit}";

    I'm guessing I would use get_popular_on_site() if I was just using one site and get_popular_on_network() if I trying to get most voted across a whole network while using multisite, right?

    Thank you again for all of your help!

  • Corbin

    After looking more into everything, I'm pretty sure I've answered all my own questions. Don't mind that last question, I really don't know how I didn't see: // Only posts on multisite sites/blogs

    I kind of figured out how shortcodes are being added in class_wdpv_codec.php
    And I'm positive that those are the queries.

    Now for just

    2. A query that I can use to display the user voted content only.

    I found the setting in the admin page Show recent votes on user profile page I went and found the file wpmu-dev-post-votes\lib\forms/bp_profile.php and the code:

    <?php foreach ($recent_votes as $vote) { ?>
    	<li>
    		<?php
    			$url = get_blog_permalink($vote['blog_id'], $vote['post_id']);
    			$post = get_blog_post($vote['blog_id'], $vote['post_id']);
    			$title = $post->post_title;
    		?>
    		<a href="<?php echo $url;?>"><?php echo $title;?></a>
    	</li>

    Would I have to

    A: Alter a query or function to organize by post type and use posts per page in get_recent_votes_by($user->id); function

    Or

    B: alter this variable $recent_votes = $this->model->get_recent_votes_by($user->id);

    Thank you for the help and sorry for the second question

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.