I'd like to query posts with the highest number of votes.

I'd like to query posts with the highest number of votes. Is there a meta_key name?

$popularpost = new WP_Query( array( 'posts_per_page' => 4, 'meta_key' => '???', 'orderby' => 'meta_value_num', 'order' => 'DESC' ) );

  • Ash
    • WordPress Hacker

    Hello @Patrick Johnson

    Welcome to WPMU community!

    I hope you are well today and thanks for asking the question.

    Would you please check following function if that works for you?

    <?php
    function getByVote() {
    global $wpdb;
    $sql = "SELECT * FROM $wpdb->posts as p join {$wpdb->prefix}wdpv_post_votes as v where v.post_id = p.ID order by v.vote DESC";
    $posts = $wpdb->get_results($querystr);
    print_r($posts); //Do whatever you want
    }
    getByVote();
    ?>

    Cheers
    Ash

  • Patrick Johnson
    • WPMU DEV Initiate

    I have it my index.php like this:

    ` function getByVote() {
    global $wpdb;
    $sql = "SELECT * FROM $wpdb->posts as p join {$wpdb->prefix}wdpv_post_votes as v where v.post_id = p.ID order by v.vote DESC";
    $popularpost = $wpdb->get_results($querystr);
    }
    getByVote();

    while ( $popularpost->have_posts() ) : $popularpost->the_post(); ?>`

    I get this error: <b>Fatal error</b>: Call to a member function have_posts() on a non-object in <b>/usr/home/taxman10m/public_html/altercate.net/wp-content/themes/red.ma.altercate.net/index.php</b> on line <b>107</b>

    I thought it might be because get_results($querystr) should be get_results($sql)? Maybe that it isn't correct because that also didn't make it work.

  • Ash
    • WordPress Hacker

    Hello there

    Use in this way :slight_smile:

    <?php
    function getByVote() {
    global $wpdb;
    $sql = "SELECT * FROM $wpdb->posts as p join {$wpdb->prefix}wdpv_post_votes as v where v.post_id = p.ID order by v.vote DESC";
    $posts = $wpdb->get_results($sql);
    return $posts;
    }
    $popular = getByVote();
    echo "<ul>";
    foreach($popular as $val) {
    ?>
    <li><a href="<?php echo get_permalink($val->ID) ?>"><?php echo $val->post_title; ?></a></li>
    <?php
    }
    echo "</ul>";
    ?>

    Let me know if it works for you.

    Cheers
    Ash

  • Patrick Johnson
    • WPMU DEV Initiate

    It isn't quite as resolved as I thought. I'm doing this on a multisite. I network enabled the Post Voting plugin.

    Your sql code pops an error.

    SELECT * FROM wp_2_posts as p join wp_2_wdpv_post_votes as v where v.post_id = p.ID order by v.vote DESC
    Table 'wp_2_wdpv_post_votes' doesn't exist

    The blog ID is 2, but there isn't an associated wpdv_2_post_votes table. The table exists as wp_post_votes.

  • Ash
    • WordPress Hacker

    Hi Patrick

    Sorry for extreme delay here. Somehow this thread is dropped from my feeds. I have extended your membership for 7 more days so that we can resolve your issue.

    I didn't understand the problem. Are you using single site or multisite? Would you please explain more about the issue?

    Cheers
    Ash

  • Ash
    • WordPress Hacker

    Okay so what the error you are seeing? Please let me know about the error. You can enable debug mode.

    Would you please enable debug mode and check if any error is shown in that page?

    To enable debug mode please edit a line in your wp-config.php:
    Change

    define('WP_DEBUG', false);

    to

    define('WP_DEBUG', true);

    Please let me know.

    Cheers
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.