22

This query works and returns the results that I want in MySQL but how do I get this to work in wordpress?

SELECT * FROM `wp_usermeta` WHERE `meta_key` = 'points' AND `user_id` = '1'

I want to be able to see all the 'points' earned by user 1. I need to be able to display this on posts and pages in wordpress.

3 Answers 3

30
global $wpdb;    
$result = $wpdb->get_results( "SELECT * FROM wp_usermeta WHERE meta_key = 'points' AND user_id = '1'");
print_r($result);

Read this.

Sign up to request clarification or add additional context in comments.

11 Comments

That article talks about making a plugin. I don't want to make a plugin just add some lines of code that will make the result appear on a page or post. I use the plugin Allow PHP in Post and Pages to add the php query code into. Is there another/easier way?
You can try add global $wpdb; before query code. It need to help, but I didn't try. I updated the code.
This is what shows up on the page when I enter your code above into the "add php plugin": Array ( [0] => stdClass Object ( [umeta_id] => 38 [user_id] => 1 [meta_key] => points [meta_value] => 1 ) [1] => stdClass Object ( [umeta_id] => 39 [user_id] => 1 [meta_key] => points [meta_value] => 100 ) [2] => stdClass Object ( [umeta_id] => 40 [user_id] => 1 [meta_key] => points [meta_value] => 66 ) [3] => stdClass Object ( [umeta_id] => 41 [user_id] => 1 [meta_key] => points [meta_value] => 32 ) etc...
I want to be able to add all the values up. I'm guessing I use Select SUM * From... When I do, I just get: array ( ) on my page
ok I got pretty close. I used: global $wpdb; $result = $wpdb->get_results( "SELECT SUM(meta_value) FROM wp_usermeta WHERE meta_key = 'points' AND user_id = '1'"); print_r($result); and got: Array ( [0] => stdClass Object ( [SUM(meta_value)] => 615 ) ) I just want the number 615. How do I get ride of the other stuff on that line.
|
2

Use following code.....

$q="SELECT * FROM wp_usermeta WHERE meta_key = 'points' AND user_id = '1'";
$result = $wpdb->get_results($q);

It will return array as result.

2 Comments

where can I even type this in though? I have a plugin called "Allow PHP in Post and Pages" and I tried the code there. This is the error it gives me: Fatal error: Call to a member function get_results() 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 2
I'm not sure what that means.
1

You should try following code :

global $wpdb;    
$result = $wpdb->get_results( "SELECT * FROM ".$wpdb->prefix."usermeta WHERE meta_key = 'points' AND user_id = '1'");
print_r($result);

Hope it will helpful.

2 Comments

usually the prefix already contains the trailing _
@DiegoBetto you are right, let me update the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.