• Resolved eddioot

    (@eddioot)


    Hi all,

    on my website users can give the posts a +1 or -1 vote.
    The sum of all the votes is stored in a variable called $postscore in each post.

    Is it possible to use this variable in the wordpress loop? My goal is to create a hotpage that displays the top rated posts from the last 2 weeks..

    I googled a lot but did not find anything in this direction..

Viewing 7 replies - 1 through 7 (of 7 total)
  • Is this value being stored in the database?

    Thread Starter eddioot

    (@eddioot)

    No there is a table in the database where i get the value from. It is the sum of all votes for the post id, my code is:

    <?php
    $post_id = $post->ID;
    $score = $wpdb->get_var( $wpdb->prepare(
                    "
                                   SELECT sum(rating_rating)
                                   FROM pp_ratings
                                   WHERE rating_postid =%s
                    ",
                    $post_id
    ) );
    $postscore = "{$score}";
    
                                                   ?>

    Then you will have to run that query for every post in the Loop.

    Thread Starter eddioot

    (@eddioot)

    Yes i put the query in the loop so every post has his $postscore .
    I was just wondering if i am able to sort the posts to be displayed in descending order for the $postscore.

    Not unless you change your approach & store this value in a custom field for each post.

    Thread Starter eddioot

    (@eddioot)

    ok, thanks for the advise. I will look into that. 🙂

    Thread Starter eddioot

    (@eddioot)

    It was indeed fixed by using custom fields.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘using php variable in loop’ is closed to new replies.