Title: Execute extra SQL on post rating
Last modified: August 20, 2016

---

# Execute extra SQL on post rating

 *  [koffers](https://wordpress.org/support/users/koffers/)
 * (@koffers)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/execute-extra-sql-on-post-rating/)
 * Hello,
 * I have been using this plugin on my site for the past two years, and have amassed
   over 450,000 total votes on 1000 posts. But we are going to be moving away from
   using this plugin to an in built function in our new theme to display ratings.
   The new function requires meta-data to be created in the wp_postmeta table. Because
   our site has quite a lot of traffic, and rates are being cast almost constantly,
   what I would like to do is edit this plugin, so that every time a vote is cast
   using WP-PostRatings, the meta data is created, or updated, the wp_postmeta table.
   I have a SQL query that can do this for each post individually, but I do not 
   have the PHP knowledge to know how to integrate this with the existing plugin.
 * I would imagine I need to store the SQL in a new function but I have no idea 
   what to do after that. Can anyone help?
 * The (complicated) SQL query is shown below. Any help is greatly appreciated.
 *     ```
       SELECT
           rating_postid
           ,'like'
           ,CONCAT
             ('a:', COUNT(rating_id), ':{',
               (SELECT CONCAT( GROUP_CONCAT(meta_data_vote SEPARATOR ''), '}')
                FROM
                 (SELECT CONCAT
                   ('i:',
                    @curRow := @curRow + 1,
                    ';a:2:{s:7:"',
                    'user_id',
                    '";s:1:"0";s:2:"ip";s:16:"',
                    '-127-000-000-001',
                    '";}'
                   ) AS meta_data_vote
                  FROM $wpdb->ratings JOIN
                    (SELECT @curRow := -1 AS j) r
                     WHERE rating_postid = @post_id
                       and rating_rating > 0
                    )AS meta_data_votes
                 )
             ) AS new_ratings_meta_data
         FROM wp_ratings l
         WHERE rating_postid = @post_id
           and rating_rating > 0);
       ```
   
 * [http://wordpress.org/extend/plugins/wp-postratings/](http://wordpress.org/extend/plugins/wp-postratings/)

The topic ‘Execute extra SQL on post rating’ is closed to new replies.

 * ![](https://ps.w.org/wp-postratings/assets/icon.svg?rev=978014)
 * [WP-PostRatings](https://wordpress.org/plugins/wp-postratings/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-postratings/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-postratings/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-postratings/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-postratings/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-postratings/reviews/)

## Tags

 * [edit plugin](https://wordpress.org/support/topic-tag/edit-plugin/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [sql](https://wordpress.org/support/topic-tag/sql/)

 * 0 replies
 * 1 participant
 * Last reply from: [koffers](https://wordpress.org/support/users/koffers/)
 * Last activity: [13 years, 2 months ago](https://wordpress.org/support/topic/execute-extra-sql-on-post-rating/)
 * Status: not resolved