skip to Main Content

I need to run a query in phpmyadmin that joins wp_users with wp_usermeta on the user_id to ID. I have the below so far.

SELECT
 wp_users.ID AS 'User ID',
 wp_users.display_name AS 'Name',
 meta_value AS 'Representative'
FROM wp_usermeta JOIN wp_users ON wp_usermeta.user_id=wp_users.ID
WHERE `meta_key` = 'representative' AND `meta_value` LIKE '%%'
ORDER BY wp_users.ID

This works (or worked) but I am not sure it is the correct to accomplish this. I need 8 different meta_key/meta_value pairs I just don’t know how to add them….

If you need more information I am happy to supply it…

2

Answers


  1. WordPress is built on mySQL and php, so I see no reason not to. Will often be faster when have lots of absraction.

    global $wpdb;    
    $result = $wpdb->get_results( "SELECT ...");
    print_r($result);
    

    It should be possible to use this too I would think:
    https://developer.wordpress.org/reference/functions/get_user_meta/

    View:

    CREATE VIEW aviw_UserMetadata
    AS
    SELECT
     wp_users.ID AS 'User ID',
     wp_users.display_name AS 'Name',
     meta_value AS 'Representative'
    FROM wp_usermeta 
    INNER JOIN wp_users ON wp_usermeta.user_id=wp_users.ID
    WHERE `meta_key` = 'representative' AND `meta_value` LIKE '%%'
    
    Login or Signup to reply.
  2. If you just looking to get value based on different keys, you can do like this:

    SELECT * FROM (
     SELECT u.ID, u.display_name, um.meta_value, um.meta_key
     FROM wp_usermeta AS um
     JOIN wp_users AS u
     ON um.user_id = u.ID
     WHERE `meta_value` LIKE '%%'
    ) AS meta
    WHERE `meta_key` = 'key1' OR `meta_key` = 'key2' OR `meta_key` = 'key3';
    

    If this is working, you can add aliases after making sure you code works.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search