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 7190073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:26:08+00:00 2026-05-28T19:26:08+00:00

I have a WordPress site which has post ratings stored in the wp_ratings table,

  • 0

I have a WordPress site which has post ratings stored in the wp_ratings table, with each rating stored as an individual row. We will be changing the theme soon and need to convert the values stored in wp_ratings to meta data, which will look as follows:

a: 3: {
i: 0;
a: 2: {
    s: 7: "user_id";
    s: 1: "0";
    s: 2: "ip";
    s: 9: "127.0.0.1";
}
i: 1;
a: 2 {
    s: 7: "user_id";
    s: 1: "0";
    s: 2: "ip";
    s: 9: "127.0.0.1";
}
i: 2;
a: 2: {
    s: 7: "user_id";
    s: 1: "0";
    s: 2: "ip";
    s: 9: "127.0.0.1";
}
i: 3;
a: 2: {
    s: 7: "user_id";
    s: 1: "0";
    s: 2: "ip";
    s: 9: "127.0.0.1";
}

The total vote value is show by the a:3 at the beginning of the statement and then there is an individual entry for each rating stored.

I am not particularly familiar with stored procedures but I want to create a loop that will calculate the total value for each post in wp_posts sum(rating_rating) and then create the meta data into one long string.

I don’t really know where to start with this, so if anyone can point me in the right direction, I’d be really grateful.

Thanks

  • 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-05-28T19:26:10+00:00Added an answer on May 28, 2026 at 7:26 pm

    Setup test data

    CREATE TABLE `wp_ratings` ( 
      `rating_id` INT(11) NOT NULL AUTO_INCREMENT, 
      `rating_postid` INT(11) NOT NULL, 
      `rating_posttitle` TEXT NOT NULL, 
      `rating_rating` INT(2) NOT NULL, 
      `rating_timestamp` VARCHAR(15) NOT NULL, 
      `rating_ip` VARCHAR(40) NOT NULL, 
      `rating_host` VARCHAR(200) NOT NULL, 
      `rating_username` VARCHAR(50) NOT NULL, 
      `rating_userid` INT(10) NOT NULL DEFAULT '0', 
      PRIMARY KEY  (`rating_id`), 
      KEY `rating_postid` (`rating_postid`) 
    ); 
    
    INSERT INTO `test`.`wp_ratings` 
      (`rating_id`, `rating_postid`, `rating_posttitle`, 
      `rating_rating`, `rating_timestamp`, `rating_ip`, 
      `rating_host`, `rating_username`, `rating_userid`)
      VALUES
      (1,1,'title',1,'abc','127.0.0.1','a.a.a','user_id',1),
      (2,2,'title',1,'abc','127.0.0.1','a.a.a','user_id',1),
      (3,2,'title',1,'abc','127.0.0.1','a.a.a','user_id',1),
      (4,3,'title',1,'abc','127.0.0.1','a.a.a','user_id',1),
      (5,3,'title',1,'abc','127.0.0.1','a.a.a','user_id',1),
      (6,3,'title',1,'abc','127.0.0.1','a.a.a','user_id',1);
    

    Query

    SET @post_id = 3;
    
    SELECT CONCAT
    (
    'a:', COUNT(rating_id), ':{',
        (
            SELECT CONCAT( GROUP_CONCAT(meta_data_vote SEPARATOR ''), '}') FROM
            (
                SELECT CONCAT
                ( 
                    'i:',
                    @curRow := @curRow + 1,
                    ';a:2:{s:7:"', 
                    rating_username, 
                    '";s:1:"0";s:2:"ip";s:9:"', 
                    rating_ip,
                    '";}'
                )AS meta_data_vote
                FROM
                    wp_ratings
                JOIN (SELECT @curRow := -1 AS j) r
                WHERE rating_postid = @post_id
            )AS meta_data_votes
        )
    ) AS new_ratings_meta_data
    FROM wp_ratings l
    WHERE rating_postid = @post_id
    

    Result (where @post_id = 3)

    a:3:{i:0;a:2:{s:7:"user_id";s:1:"0";s:2:"ip";s:9:"127.0.0.1";}i:1;a:2:{s:7:"user_id";s:1:"0";s:2:"ip";s:9:"127.0.0.1";}i:2;a:2:{s:7:"user_id";s:1:"0";s:2:"ip";s:9:"127.0.0.1";}}
    

    Conclusion

    I haven’t been able to write a query that returns a resultset with post_ids and their new meta_data.

    The above code will only convert on a post-by-post basis.

    If you want to batch update, it’ll need more thought, but i think this will get you off to a good start.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a wordpress site with 5k post and each post has average 25
I have a WordPress site (2.6.2) in which I have set the Home page
I have a WordPress website ( abetterworldbydesign.com ) which has user-agent detection to redirect
I have a WordPress site, in which type1 users can upload *.doc files and
I have a wordpress site. I have a twitter button below every post. What
I have a Wordpress site and WP supplies a .htaccess which includes the following:
I have a WordPress site. There is some AdSense code in it which I
I have a 2 sites integrated into one, wordpress is my second site which
I have a site which is already part built and has some custom features
I have a web site which will be featured in a place which will

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.