Ad 468 X 60

Monday, 19 May 2014


Select usermeta table from multiple sets of meta keys and their values in wordpress

Dear WordPress developers,
This is our second post of this blog ,and this post is about WordPress complex MySQL query.Actually i am talking about some scenarios in WordPress when we want to get result from user-meta  table and some time our requirement is out scope of WordPress MySQL function. like we want to get result from user-meta table with more than one meta keys and meta values at that time WordPress MySQL function will not produce exact results so we have to customize the MySQL query to get  the result..

Now Just use WordPress Query Function for Wp User Meta Table

// define global variable 

global $wpdb;  
$sql= $wpdb->get_results("
select * from wp_usermeta a1 inner join wp_usersmeta
a2 on a1.user_id=a2.user_id 
where (a1.meta_key="something" and a1.meta_value="something")
and(a2.meta_key="something" and a2.meta_value="something")
//Now  Output will  

This query will return a stdClass object .


  • Facebook
  • Twitter
  • Myspace
  • Google Buzz
  • Reddit
  • Stumnleupon
  • Delicious
  • Digg
  • Technorati

1 comment:

  1. hello , i have a same problem
    request :
    select a1.meta_value,a2.meta_value,a3.meta_value from usermeta a1
    inner join usermeta a2 on a1.user_id=a2.user_id
    inner JOIN usermeta a3 ON a2.user_id=a3.user_id
    where (a1.meta_key="job_listing_category"
    and a2.meta_key="metier_domaine"
    and a3.meta_key="description"
    and a1.user_id=4)

    Normally the result must give two lines no more, but it loops and binds the a1.meta_value, a2.meta_value, a3.meta_value