Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7935393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:54:23+00:00 2026-06-03T21:54:23+00:00

I need some help with a query that should return posts based on their

  • 0

I need some help with a query that should return posts based on their wp-postratings score (http://wordpress.org/extend/plugins/wp-postratings/).

The user chooses a minimum rating (0 to 5 stars) and a maximum rating (0 to 5 stars) and the query should return the posts that match. I have it working where the user input for both values is above 0 but I can’t seem to get my head around the 0 value. Since 0 represents unrated posts – and hence onces that have no ratings meta data – I need to select not only the posts where the rating is no more than the specified max value, but also every post that has no rating meta data.

How can I do this?? Any help will be very much appreciated!

Here’s my current query:

SELECT DISTINCT p.*, (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users, (t3.meta_value+0.00) AS ratings_score 
FROM wp_posts p 
INNER JOIN wp_term_relationships tr ON (p.ID = tr.object_id) 
INNER JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) 
INNER JOIN wp_terms t ON  t.term_id = tt.term_id
LEFT JOIN wp_postmeta AS t1 ON t1.post_id = p.ID 
LEFT JOIN wp_postmeta AS t2 ON t1.post_id = t2.post_id 
LEFT JOIN wp_postmeta AS t3 ON t3.post_id = p.ID 
WHERE t1.meta_key = 'ratings_average' 
AND t2.meta_key = 'ratings_users' 
AND t3.meta_key = 'ratings_score' 
AND p.post_date < NOW() 
AND p.post_status = 'publish' 
AND (tt.taxonomy = 'post_tag' AND tt.term_id = t.term_id AND t.slug = 'liverpool')
AND ( (t1.meta_value+0.00) IS NULL OR (t1.meta_value+0.00) <= $max_stars )
ORDER BY p.post_date DESC 
LIMIT 20
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:54:25+00:00Added an answer on June 3, 2026 at 9:54 pm

    Okay, this query seems to work for me. Its a bit ugly though and not too quick so if anyone has a better one feel free to improve upon it!

    It selects all of the rated posts that are below the $max_stars value, then combines the table with a separate select which gets all of the non-rated posts:

    (SELECT DISTINCT p.*, (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users, (t3.meta_value+0.00) AS ratings_score 
    FROM wp_posts p
    INNER JOIN wp_term_relationships tr ON (p.ID = tr.object_id) 
    INNER JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) 
    INNER JOIN wp_terms t ON  t.term_id = tt.term_id
    LEFT JOIN wp_postmeta AS t1 ON t1.post_id = p.ID 
    LEFT JOIN wp_postmeta AS t2 ON t2.post_id = p.ID 
    LEFT JOIN wp_postmeta AS t3 ON t3.post_id = p.ID 
    WHERE t1.meta_key = 'ratings_average' 
    AND t2.meta_key = 'ratings_users' 
    AND t3.meta_key = 'ratings_score' 
    AND p.post_date < NOW() 
    AND p.post_status = 'publish' 
    AND (tt.taxonomy = 'post_tag' AND tt.term_id = t.term_id AND t.slug = 'liverpool')
    AND (t1.meta_value+0.00) <= $max_stars )
    
    UNION
    
    (SELECT DISTINCT p.*, NULL AS ratings_average, NULL AS ratings_users, NULL AS ratings_score 
    FROM wp_posts p 
    INNER JOIN wp_term_relationships tr ON (p.ID = tr.object_id) 
    INNER JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) 
    INNER JOIN wp_terms t ON  t.term_id = tt.term_id
    LEFT JOIN wp_postmeta AS t1 ON (t1.post_id = p.ID AND t1.meta_key = 'ratings_score')
    WHERE t1.post_id is null
    AND p.post_date < NOW() 
    AND p.post_status = 'publish' 
    AND (tt.taxonomy = 'post_tag' AND tt.term_id = t.term_id AND t.slug = 'liverpool') )
    
    ORDER BY post_date DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help a query. For each movie in my database that has
I need some help with a T-SQL query. I want to count fields that
I really need some help with forming a MySQL query that I just cannot
I am new to SQL Server 2008, and I need some help in query
Need some help with a query.. I have three tables. Source id name 1
I need some help with SQL Query. I am trying to select all records
I'm working on a web application and need some help with a query. I'm
I need some help putting together this query in Django. I've simplified the example
I need some help with a LINQ query in VB.Net, please. I have this
I need some help adding conditions to a linq query. Take this small example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.