mysql - Combining mutliple Wordpress database queries -
forgive me re-wording , re-asking question, answers received couple weeks didn't much...
basically, i'm looking somehow combine multiple database queries in wordpress retrieve user ids searching term in 'usermeta' table, entries have 'meta_value'
i'm trying combine:
$users = $wpdb->get_results("select user_id, meta_value 'business_name' $wpdb->usermeta meta_key = 'business_name'");
and:
$users = $wpdb->get_results("select user_id, meta_value 'business_description' $wpdb->usermeta meta_key = 'business_description'");
to have this:
$users = $wpdb->get_results("select user_id, business_name, business_description business_name '%{$keyword}%' or business_description '%{$keyword}%'");
i've looked inner joins , subqueries, cannot seem find solution. realize can away multiple queries, searching through possibly thousands of entries, i'd optimize as possible.
hi @john:
if understand question correct (since gave technical example of attempting not description of end result trying accomplish i'm not sure) seems doing user search? if yes, below think need.
<?php // load wordpress, standalone example use include '../wp-load.php'; // make sure database object available global $wpdb; // value entered user $keyword = $_get['keyword']; // used in code; percent confuse prepare() below $keyword = "%{$keyword}%"; // join each meta field separate join , reference meta_key in join // prepare() below replaces "%s" quoted string value $sql =<<<sql select users.user_nicename user_name, bizname.meta_value business_name, bizdesc.meta_value business_description {$wpdb->users} users inner join {$wpdb->usermeta} bizname on bizname.user_id=users.id , bizname.meta_key='business_name' inner join {$wpdb->usermeta} bizdesc on bizdesc.user_id=users.id , bizdesc.meta_key='business_description' 1=0 or bizname.meta_value %s or bizdesc.meta_value %s sql; // user prepare() avoid sql injection hacks $sql = $wpdb->prepare($sql,$keyword,$keyword); // finally, yer results! $users = $wpdb->get_results($sql); echo "<ul>"; foreach($users $user) { echo "<li>user= {$user->user_name}, business= {$user->business_name}:{$user->business_description}</li>"; } echo "<ul>";
the above complete working example can copy file in root of website , call /test.php
allowing see work using url this:
http://example.com/test.php?keyword=accounting
of course, may less performant @ times using multiple queries because of query caching systems built-in wordpress it's impossible tell without benchmarking.
hope helps.
-mike
p.s. way, i'm assuming not aware of since prior question evidently hadn't gotten wordpress love nor had 1 i'll mention wordpress answers website sister site stackoverflow. lots of wordpress enthusiasts on hand on there answer wordpress-specific questions.my experience stackoverflow have of best developers on web few here have specific experience developing wordpress end people here trying answer mysql questions without knowing wordpress database schema , without knowing wordpress-specific best practices. ask on @ wordpress answers , think you'll improved quality of answers wordpress-specific questions.
Comments
Post a Comment