Jump to content

Recommended Posts

In WordPress I need a listing generated from table wp_usermeta which shows following results per row per user_id :
user_id, city, first_name+ " "+lastname


This is an example of the data in table wp_usermeta.

126 city Utrecht
126 last_name Huisman
126 first_name Fred
100 city Eibergen
100 last_name Van der Heide
100 first_name Toon
90 city Hengelo
90 last_name Klaver
90 first_name Gezien

What I need is following result:
126 Utrecht Fred Huisman
100 Eibergen Toon Van der Heide
90 Hengelo Gezien Klaver

Below script is wors. But it give you an idea of the table _ fields:

SELECT wp_users.ID, wp_usermeta.meta_key , wp_usermeta.meta_value
FROM wp_users INNER JOIN wp_usermeta ON wp_users.ID = wp_usermeta.user_id
WHERE
wp_usermeta.meta_key = "city"
OR
wp_usermeta.meta_key = "first_name" + "last_name"
OR
(wp_usermeta.meta_key = "lastwill_public" AND wp_usermeta.meta_value = "1")
AND wp_usermeta.meta_value is null
ORDER BY wp_users.ID DESC

 

Who can help me  getting the result.
In advance thanks!

Link to comment
https://forums.phpfreaks.com/topic/297768-wordpress-wp_usermeta-listing-problem/
Share on other sites

Something like this will do it. (It isn't pretty, but then neither is the wp_usermeta table).

SELECT u.id
 , firstname.meta_value
 , lastname.meta_value
 , city.meta_value
FROM wp_users u
INNER JOIN
	(
	SELECT u.id
	  ,	m.meta_value
	FROM wp_users u
	LEFT JOIN wp_usermeta m ON u.ID=m.user_id
		AND m.meta_key = 'firstname'
	) firstname USING (id)
INNER JOIN
	(
	SELECT u.id
	  ,	m.meta_value
	FROM wp_users u
	LEFT JOIN wp_usermeta m ON u.ID=m.user_id
		AND m.meta_key = 'lastname'
	) lastname  USING (id)
INNER JOIN
	(
	SELECT u.id
	  ,	m.meta_value
	FROM wp_users u
	LEFT JOIN wp_usermeta m ON u.ID=m.user_id
		AND m.meta_key = 'city'
	) city USING (id)
ORDER BY u.id;

I solved this with creating an extra table for addresses. The WP-usermeta table sucks. The idea of Guru was nice but not functional.

WIth creating new table the insert + update is very easy and to get the data from the table is also better.

Thanks for the reply.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.