• The code below works but only where user_id = 1. I need to replace 1 with current user.

    global $wpdb;
    $result = $wpdb->get_results( “SELECT SUM(meta_value) AS sum FROM wp_usermeta WHERE meta_key = ‘points’ AND user_id = ‘1’”);
    echo $result[0]->sum;

    I want the resulting number (sum of points for the current user) to be displayed on posts or pages and I’m using a “add php to post and pages” plugin to do so.

    My attempt at solving this is:

    if ( is_user_logged_in() ) {
    $current_user = wp_get_current_user();
    global $wpdb;
    $result = $wpdb->get_results( “SELECT SUM(meta_value) AS sum FROM wp_usermeta WHERE meta_key = ‘points’ AND user_id = ‘$current_user'”);
    echo $result[0]->sum;}

    However, I get this error: Catchable fatal error: Object of class WP_User could not be converted to string in /homepages/8/d195441424/htdocs/test/wp-content/plugins/allow-php-in-posts-and-paβ€Œβ€‹ges/allowphp.php(373) : eval()’d code on line 4

    Is my way of adding current user close to correct? Does anyone know how to fix this?

    Help is much appreciated.

Viewing 15 replies - 1 through 15 (of 17 total)
  • That’s actually a very easy error to fix.

    The error message gives you a fair bit of info as to what the actual problem is:

    Object of class WP_User could not be converted to string

    When you get the value back from wp_get_current_user() you’re getting a WP_User object, not the users ID. That’s the important part. That object can’t be used as a string in your SQL query because it doesn’t have anything set up to output only the ID like that.

    The only thing that you need ot change is to use the ID instead of the object:

    $result = $wpdb->get_results( "SELECT SUM(meta_value) AS sum FROM wp_usermeta WHERE meta_key = 'points' AND user_id = " . $current_user->ID);

    Thread Starter bleedblue07

    (@bleedblue07)

    Awesome!!!! That works! Thanks very much for your help. I’m gonna press my luck and ask you another question. How would I limit someone to getting points just 1 time. For example, to get this thing working I have it set up so a user gets 20 points for going to a certain page, say the homepage. I don’t want them getting 20 points for that every time, just the first time. Or say 5 points if they vote on something. I don’t want them casting 10 votes and getting 50 points, just the 1. Any ideas of how to go about doing this? Where to start?

    I would guess that an easy way would be to have the meta key set up with the page ID so every time it tried to add it, it will only add points to that page’s meta entry. As an example…

    $page_points = 10;
    
    update_user_meta ($user->ID, 'points_' . $post->ID, $page_points);

    Then you can SUM() the points using a LIKE statement in your SQL:

    $result = $wpdb->get_results( "SELECT SUM(meta_value) AS sum FROM wp_usermeta WHERE meta_key LIKE 'points_%' AND user_id = " . $current_user->ID);

    That query will take a fair bit longer to process but you could set up an index on the meta_key column in your database to help speed things up a bit.

    There’s other ways to do that as well, like creating your own table and storing the points against the user ID and post ID, but that’s adding a bit more complexity into it, evne though in the long run it would be a better system.

    Thread Starter bleedblue07

    (@bleedblue07)

    This is how I’m giving them points:

    if ( is_user_logged_in() ) {
    $current_user = wp_get_current_user();
    $meta_value = 32;
    add_user_meta($current_user->ID, ‘points’, $meta_value, $unique);
    }
    ?>

    I need it to somehow say if user is logged in & hasn’t already gotten points for this.

    You’re not doing it the way that I suggested before. πŸ™‚

    Look back at the meta_key that I give the entry and you’ll see where I’ve made it different. It’s not just points. It’s points_5 or whatever the page ID is. That’s where the difference is. That way the system will know what page the points have been given for and there will only be one entry for each user for each page and the points won’t be added as you’re only updating them each time, not adding them.

    Thread Starter bleedblue07

    (@bleedblue07)

    I posted my follow up before I realized you responded. You’re too quick haha.

    I’ll take a look at it for sure. I’m still pretty novice at this stuff so it’ll take me a bit to understand. Will your way work for things like submit buttons or clicking links? Or will that just work for pages.

    It can work for anything where you can give it a unique ID. You can also change the meta_key to something like points_click_45 and as long as your SQL query uses LIKE 'points_%' it will sum everything that’s been set.

    But as I said closer to the start, I think that you’d be better off looking at a different solution. If it was me, I’d set up a new database table and use that to collect the points for each user. The biggest reasons for this are speed and encapsulation. Using the SQL that I’ve given you there will work, but will be slow (all ‘LIKE’ clauses are slow compared to correctly indexed look ups). It will also keep the points in one area and no thave them mixed in with everything else. There’s also the chance that some other plugin that you use in the future may want to use points_ for something to do with users as well, and that will break your own system.

    Unfortunately there’s always more then one way to do something like this, but I’d actually tell you that ths is a good opportunity to learn more and expand what you think you can do. It’s all possible and actually not that hard when you get down to it.

    Thread Starter bleedblue07

    (@bleedblue07)

    Ha. Still hard for me. But I’ll look into making a new points database. If using LIKE slows everything down as you said then I’m better off trying to do it the right way. Hopefully I can attempt this soon. Again thanks for your help.

    Thread Starter bleedblue07

    (@bleedblue07)

    You mentioned to create my own table and store the points against the user ID and post ID. What does that mean to store against the user ID? How do I do that?

    I set up a new table exactly (hopefully) the same as the usermeta table. I’m also not sure how to add things to that table like we did before with add_user_meta. I tried that with my mine: add_userpoints hoping that it would add to my userpoints table but it did not.

    You wouldn’t set it up the same as the meta table. That’s the wrong srot of model to work off for what you’re trying to do.

    The sort of table that I’m talking at is like this:

    CREATE TABLE IF NOT EXISTSwp_user_points` (
    user_id bigint(20) unsigned NOT NULL,
    post_id bigint(20) unsigned NOT NULL,
    points int(11) NOT NULL,
    PRIMARY KEY (user_id,post_id)
    );`

    That uses the user ID and the post/page ID as a compound primary key, so when you add the points in, you’ll only ever get one entry for each page for each user. It’s easier to get the totals as well.

    To insert is something like this:

    $query = "INSERT INTO wp_user_points (user_id, post_id, points) VALUES (".$user_id.", ".$post_id.", ".$points.") ON DUPLICATE KEY UPDATE points = ".$points."

    The important part on this is ON DUPLICATE KEY UPDATE. That tells MySQL to update the value if the primary key already exists, meaning that there’s always one record only for that user/post.

    One thing to remember with that is that I haven’t done any escaping of the values, so be sure to add that in for yourself as it’s needed.

    That’s the basics behind it, but that’s more then enough to get you going.

    Thread Starter bleedblue07

    (@bleedblue07)

    When you say:

    CREATE TABLE IF NOT EXISTSwp_user_points` (
    user_id bigint(20) unsigned NOT NULL,
    post_id bigint(20) unsigned NOT NULL,
    points int(11) NOT NULL,
    PRIMARY KEY (user_id,post_id)
    );`

    Are you saying I should create a new table with this criteria? Can I paste that code in somewhere and it will do that for me? Sorry I’m big time rookie.

    You can paste that into phpMyAdmin and it will create that table. If you are using something else you can always use that as a description and add it manually.

    Thread Starter bleedblue07

    (@bleedblue07)

    I’ve had a little time to do some research but am still having trouble. Before I tried to get the code you posted above to work I just wanted to to be able to insert anything to any table. Yet somehow that isn’t working either.

    I found a site that explains $wpdb and I copied a code from there exactly except changed the user_id from 1 to 4. This is the code:

    $wpdb->insert($wpdb->usermeta, array(“user_id” => 4, “meta_key” => “awesome_factor”, “meta_value” => 10), array(“%d”, %s”, “%d”));
    ?>

    This is the error:
    Parse error: syntax error, unexpected ‘%’, expecting ‘)’ in /homepages/8/d195441424/htdocs/test/wp-content/plugins/allow-php-in-posts-and-pages/allowphp.php(373) : eval()’d code on line 1

    I’m trying to google the errors I get first but I can’t seem to figure it out. Any ideas?

    Also I couldn’t find anything on this part of your code VALUES (“.$user_id.”, “.$post_id.”, “.$points.”) Why do you have . inside the ” “?

    I’ll start with the easy part. The .’s inside the strings are like “joiners”. When you use a full-stop like that, it means join one string to another string. As an example:

    $a = "Hello";
    $b = "World";
    echo $a." ".$b."!";

    Would output:

    Hello World!

    As for your error, that’s an easy one. Look at this bit of code carefully:

    array("%d", %s", "%d")

    You’re missing the opening ” in front of %s. So, it should be:

    array("%d", "%s", "%d")

    As for you trying to insert the values like that, i’d suggest not doing it that way. use the standard WordPress functions that are already built and ready to go:

    update_user_meta ($user->ID, "key_name", 10)

    BUT… That’s if you want ot do it the old way. If you’re doing it my suggested way you’re not going to be inserting anything into the user_meta table.

    Thread Starter bleedblue07

    (@bleedblue07)

    Ya I’ll be inserting into the new wp_user_points table I created. I just wanted to copy and paste the exact code to make sure that even worked haha. Which I wish I could say does work with adding the missing “. Now it gives me this error:

    Fatal error: Call to a member function insert() on a non-object in /homepages/8/d195441424/htdocs/test/wp-content/plugins/allow-php-in-posts-and-pages/allowphp.php(373) : eval()’d code on line 1

    I understand your hello world example so thanks for that. But with your code: $query = “INSERT INTO wp_user_points (user_id, post_id, points) VALUES (“.$user_id.”, “.$post_id.”, “.$points.”) ON DUPLICATE KEY UPDATE points = “.$points.” … I’m still confused. Since we’re trying to insert and not echo why do we need the values joined. Don’t we want them to each go into there own column?

Viewing 15 replies - 1 through 15 (of 17 total)

The topic ‘SUM(meta_value) for current user’ is closed to new replies.