select query in wordpress - php

I am trying to do this by 2 hours.
I have custom fields in database and I want to get post_id by the meta keys or meta values.
I am doing like this
$post_id = $wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE (meta_key = 'mfn-post-link1' AND meta_value = '". $from ."')");
print_r($post_id); // giving only 140
this is working fine, but this is giving only one post_id and I want all possible post_id matched by meta_value.
for example :
I have three post 140,141,142, in database. But by this query I am only getting 140.
Any Idea how to get all possible post_id by this query or any other way by comparing meta_fields...
Thanks

$post_id = $wpdb->get_results("SELECT post_id FROM $wpdb->postmeta WHERE (meta_key = 'mfn-post-link1' AND meta_value = '". $from ."')");
print_r($post_id); /

$post_id = $wpdb->get_results("SELECT post_id FROM $wpdb->postmeta WHERE (meta_key = 'mfn-post-link1' AND meta_value = '". $from ."')");
print_r($post_id);
for more information ... http://codex.wordpress.org/Class_Reference/wpdb

<?php
$user_id = 1;
global $wpdb;
$wpdb_prefix = $wpdb->prefix;
$wpdb_tablename = $wpdb_prefix.'Table_Name';
$result = $wpdb->get_results(sprintf('SELECT `colum1`, `colum2` FROM `%2$s` WHERE `user_id` = %d LIMIT 1', $user_id, $wpdb_tablename));
print_r($result); exit;
?>

Related

I'm struggling with the WHERE IN query

I am not able to use this code. I've echoed the implode (and it works perfectly) but I cannot seem to get it to work with the WHERE IN query.
I've tried echo the import, which works, and the code works perfectly when I remove the WHERE IN, so I know the elements within 'etc etc' are working, too.
$filter_category = "SELECT * FROM `youcard_wp`.`bc_term_relationships`
WHERE term_taxonomy_id = '57'";
$result_c = $conn90->query($filter_category);
while($row_c = $result_c->fetch_assoc()){
$category_filter[] = $row_c['object_id'];
}
echo implode(',', $category_filter); // <- works great
$get_offer_list = "
SELECT * FROM `youcard_wp`.`bc_posts`
WHERE `ID` IN ('".implode(',', $category_filter)."')
AND post_status = 'publish'
AND post_type = 'portfolio'
ORDER BY menu_order ASC
";
$result = $conn90->query($get_offer_list);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
etc etc etc...
You don't need any of that. Instead use this...
SELECT x.columns
, x.you
, x.actually
, x.want
FROM bc_posts x
JOIN bc_term_relationships y
ON y.object_id = x.id
WHERE x.post_status = 'publish'
AND x.post_type = 'portfolio'
AND y.term_taxonomy_id = 57
ORDER
BY x.menu_order ASC
You are surrounding the whole IN list with a pair of single quotes, whereas you would need to quote each individual value.
Consider:
$get_offer_list = "
SELECT *
FROM `youcard_wp`.`bc_posts`
WHERE
`ID` IN ('" .implode("','", $category_filter)."')
AND post_status = 'publish'
AND post_type = 'portfolio'
ORDER BY menu_order ASC
";
If IDs are actually numeric values and not strings, then you do not need quotes at all:
$get_offer_list = "
SELECT *
FROM `youcard_wp`.`bc_posts`
WHERE
`ID` IN (" .implode(",", $category_filter).")
AND post_status = 'publish'
AND post_type = 'portfolio'
ORDER BY menu_order ASC
";

Query not checking one field properly

I have a query that should check input order number, first name and post number and check if they match what is in the wordpress database, but the query always goes through no matter what post number I input, the code itself looks like this:
// Taking the user input into variables
$ordernumber = $_POST['ordernmbr'];
$orderfirstname = $_POST['firstname'];
$orderpostnumber = $_POST['postnmbr'];
$page = $_POST['page'];
// Sanitizing
$ordernumber = stripslashes_deep($ordernumber);
$orderfirstname = stripslashes_deep($orderfirstname);
$orderpostnumber = stripslashes_deep($orderpostnumber);
// Query that searches for order data from db
$sql = $wpdb->prepare("SELECT post_id FROM wp_postmeta
WHERE post_id = %d AND meta_key in ('_billing_first_name', '_billing_postcode')
and meta_value in ('%s', '%d' )
group by post_id", $ordernumber, $orderfirstname, $orderpostnumber);
$res = $wpdb->get_results($sql, ARRAY_A);
What am I missing so that the post number would also be required to be correct

Buddypress custom activity loop and query function

I added an additional activity feed to my profile page that queries for a linked user and returns their activity. The problem is, I want to write this over to a function as there will often be other areas of the site that need to use this same query.
Here is the user query. It's just a comparison query to find and return the linked user's ID.
global $bp, $wpdb;
$myid = $bp->loggedin_user->id;
$ourlink = $wpdb->get_var("SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = 'linkid' AND user_id = '$myid'");
$partnerid = $wpdb->get_var("SELECT user_id from $wpdb->usermeta where meta_key='linkid' AND meta_value = '$ourlink' AND user_id != '$myid'");
$partner = '&user_id=' . $partnerid;
Which is then used in the activity loop.
<?php if ( bp_has_activities( bp_ajax_querystring( 'activity' ) . $partner ) ) : ?>
However, rewriting this as a function and then calling that function right before the loop, though it will still return the ID, it no longer works for the sake of the activity loop.
Function: (in WP functions.php file)
function sw_partner_id() {
global $bp, $wpdb;
$myid = $bp->loggedin_user->id;
$ourlink = $wpdb->get_var("SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = 'linkid' AND user_id = '$myid'");
$partnerid = $wpdb->get_var("SELECT user_id from $wpdb->usermeta where meta_key='linkid' AND meta_value = '$ourlink' AND user_id != '$myid'");
$partner = '&user_id=' . $partnerid;
return $partner;
}
Calling the function before the activity loop
<?php sw_partner_id(); ?>
<?php if ( bp_has_activities( bp_ajax_querystring( 'activity' ) . $partner ) ) : ?>
Error Update:
If you want to call a function inside your functions.php file use hooks.Please notice global bp in your code since you forgot to add a $ sign.
Example:
function sw_partner_id() {
global $bp, $wpdb;
$myid = $bp->loggedin_user->id;
$ourlink = $wpdb->get_var("SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = 'linkid' AND user_id = '$myid'");
$partnerid = $wpdb->get_var("SELECT user_id from $wpdb->usermeta where meta_key='linkid' AND meta_value = '$ourlink' AND user_id != '$myid'");
$partner = '&user_id=' . $partnerid;
return $partner;
}
//sample hook
add_action('admin_init','sw_partner_id');
After that try calling the function.
I hope that helps.

My query returns null in wordpress and I don't know why

What's wrong with this query? It always returns NULL.
$recipes = $wpdb->get_results(
"SELECT ID FROM ".$wpdb->posts." WHERE post_author = %d AND post_status IN ('draft','publish') AND post_type = 'recipes' ", $current_user->ID
);
get_results takes output type as second parameter. You're missing prepare method if you want to do it like this. It should be something like
$recipes = $wpdb->get_results($wpdb->prepare ("SELECT ID FROM ".$wpdb->posts."
WHERE post_author = %d
AND post_status IN ('draft','publish')
AND post_type = 'recipes' ", $current_user->ID));
Code:
global $post;
global $wpdb;
$sel_query = "SELECT id FROM ".$wpdb->prefix."posts WHERE post_author = ".$current_user->ID." AND post_status IN ('draft','publish') AND post_type = 'recipes' ";
$totaldata = $wpdb->get_results($sel_query);
return $totaldata;

Combining multiple MYSQL queries into one with PHP

I know this subject has been covered before, and I've read about a dozen of the links provided by stackoverflow. None match my need.
I have 4 mysql queries using PHP for similar data, I'd like to lower that to one query and maybe put the results in an array that I can access. Here is my current code.
$id = $row[post_id];
$resulttwo = mysql_query("SELECT meta_value FROM wp_postmeta WHERE `post_id` = $id AND `meta_key` = 'length' ");
$temptwo = mysql_fetch_array($resulttwo);
$length[$id] = $temptwo[0];
$id = $row[post_id];
$resultthree = mysql_query("SELECT meta_value FROM wp_postmeta WHERE `post_id` = $id AND `meta_key` = 'location_city' ");
$tempthree = mysql_fetch_array($resultthree);
$trailcity[$id] = $tempthree[0];
$id = $row[post_id];
$resultfour = mysql_query("SELECT meta_value FROM wp_postmeta WHERE `post_id` = $id AND `meta_key` = 'location_state' ");
$tempfour = mysql_fetch_array($resultfour);
$trailstate[$id] = $tempfour[0];
$id = $row[post_id];
unset($tempfour);
$resultfour = mysql_query("SELECT meta_value FROM wp_postmeta WHERE `post_id` = $id AND `meta_key` = 'difficulty' ");
$tempfour = mysql_fetch_array($resultfour);
$difficulty[$id] = $tempfour[0].' difficulty';`
This should work:
$id = $row[post_id];
$result = mysql_query("SELECT meta_key, meta_value FROM wp_postmeta WHERE `post_id` = $id AND `meta_key` IN ('length', 'location_city', 'location_state', 'difficulty')");
$temp = mysql_fetch_assoc($result);
Array $temp will contain the meta_key along with the meta_value which you should be able to call like so $temp[length]. You can check the entire array with print_r($temp);
You should also stop writing new code using mysql_ functions as they are being deprecated and use mysqli_ or PDO instead.
This should be sufficient as you only care about the first row in each query.
SELECT meta_value FROM wp_postmeta WHERE post_id = $id AND meta_key IN ("length", "location_city", "location_state", "difficulty") LIMIT 4;
Damn too late! :/

Categories