Update Query not updating the data in mysql (wordpress) - mysql

I am working on freelancing site on Wordpress, fetching data from post type 'projects' and when user select the projects then project bidder will show just have to update the bid form the freelancer (bidders)
$query_update=($wpdb->prepare("
update wp_postmeta pm
join wp_posts p
on p.id = pm.post_id
join wp_users u
on u.id = p.post_author
set pm.meta_value = '$bid'
where pm.meta_key = 'bid_budget'
and p.post_title = '$project'
and u.user_login = '$user'
"));
if($query_update > 0){
echo "Successfully Updated";
}
else{
echo "Error ! Wrong Query";
echo 'Project '.$project.'<br>';
echo 'Bid '.$bid.'<br>';
echo 'User '.$user;
}
The data in the mysql backend is not changing. Is there something wrong with my query?

Thanks I got the solution I am using the wrong query - the right one is as follows:
$query_update=$wpdb->query($wpdb->prepare("update wp_postmeta pm inner join wp_posts p on p.id = pm.post_id inner join wp_users u on u.id=p.post_author set pm.meta_value = '$bid' where pm.meta_key = 'bid_budget' and p.post_title='$project' and u.user_login='$user'"));

Related

try to count this online user's order which is processing

I want to limit each user to register only one order per day.
To do this, I need to check each user's order before registering whether this user has any 'wp-processing' order or not?
I wrote below MySQL query in PHP, but it doesn't work.
I printed value of '$sql_count_from_status' and try it in phpmyadmin, SQL tab to find the error.
Value of the variable was:
SELECT count(ID) FROM wp_posts p LEFT JOIN wp_postmeta m ON p.ID=m.post_id WHERE p.post_status LIKE 'wc-processing' AND p.post_type LIKE 'shop_order' AND m.meta_key LIKE '_customer_user' AND m.meta_value LIKE '100577';
and showed me zero in phpmyadmin as result. Now I know the query has problem, because I had 'processing order' for user 100577.
function get_orders_count_from_status( $status , $this_user_id){
global $wpdb;
// We add 'wc-' prefix when is missing from order staus
$status = 'wc-' . str_replace('wc-', '', $status);
$sql_count_from_status = "
SELECT count(ID) FROM {$wpdb->prefix}posts p
LEFT JOIN {$wpdb->prefix}postmeta m
ON p.ID=m.post_id
WHERE p.post_status LIKE '$status' AND p.post_type LIKE 'shop_order'
AND m.meta_key LIKE '_customer_user'
AND m.meta_value LIKE '".$this_user_id."';";
return $wpdb->get_var($sql_count_from_status);
}
Here's an example of a plausible query (although counting on a LEFT JOIN is a bit strange):
SELECT count(*)
FROM {$wpdb->prefix}posts p
LEFT
JOIN {$wpdb->prefix}postmeta m
ON m.post_id = p.ID
AND m.meta_key = '_customer_user'
AND m.meta_value = :this_user_id
WHERE p.post_status LIKE :status
AND p.post_type = 'shop_order'

Wordpress Woocommerce Subscription - Get Total Active Subscriptions By Product

We are building a simple dashboard status plugin that will show the total "Active" subscriptions for a specific product.
I'm using the following function to grab the total active subscriptions, but the numbers aren't lining up due to switched subscriptions. How do I account for the switch subscriptions?
function get_active_subscriptions_count( $product_id) {
global $wpdb;
// return the active subscriptions for a defined product ID
return $wpdb->get_var("
SELECT COUNT(p.ID)
FROM {$wpdb->prefix}posts as p
LEFT JOIN {$wpdb->prefix}posts AS p2 ON p.post_parent = p2.ID
LEFT JOIN {$wpdb->prefix}postmeta AS pm ON p2.ID = pm.post_id
LEFT JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON pm.post_id = woi.order_id
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
WHERE p.post_type LIKE 'shop_subscription'
AND p2.post_type LIKE 'shop_order' AND woi.order_item_type LIKE 'line_item'
AND pm.meta_key LIKE '_customer_user'
AND woim.meta_key = '_product_id'
AND woim.meta_value = '$product_id'
AND p.post_status LIKE 'wc-active'
");
}
What can I do to account for the switch subscriptions? What am I missing in the query?

Get Woocommerce customer order language

I'm developing a complementary plugin for woocommerce.
I have a sql request that gets all the order and customer info, but i need to get the language from the order.
How can i detect the language was using a customer when he made an order? Is it registered somewhere?
In other CMS like prestashop it's stored as id_lang in orders and customer tables.
Without getting into which plugin you will chose and how it operates, here is how you would save some extra data to the order.
// save the extra field when checkout is processed
function kia_save_extra_checkout_fields( $order_id, $posted ){
$language = detect_language_with_your_plugin_of_choice() ? detect_language_with_your_plugin_of_choice() : "en";
update_post_meta( $order_id, '_order_language', $language );
}
add_action( 'woocommerce_checkout_update_order_meta', 'kia_save_extra_checkout_fields', 10, 2 );
And because I had an awful time the one time I tried to use WPML, maybe consider checking out Multilingual Press.
Finally solved, both solutions are OK.
With WPML plugin you can get the value in the table postmeta with the meta_key = wpml_language
Just added a left join in my SQL query:
SELECT O.ID as id_order, O.post_date_gmt as date, M.meta_value as email, M2.meta_value as firstname, M3.meta_value as lastname, M4.meta_value as iso_code
FROM ".$prefix."posts O
LEFT JOIN ".$prefix."postmeta M ON M.post_id = O.ID AND M.meta_key = '_billing_email'
LEFT JOIN ".$prefix."postmeta M2 ON M2.post_id = O.ID AND M2.meta_key = '_billing_first_name'
LEFT JOIN ".$prefix."postmeta M3 ON M3.post_id = O.ID AND M3.meta_key = '_billing_last_name'
LEFT JOIN ".$prefix."postmeta M4 ON M3.post_id = O.ID AND M4.meta_key = 'wpml_language'
WHERE O.post_type = 'shop_order' AND O.post_status = 'wc-completed'

Yield calculation in MySQL

I'm trying to calculate the Yield based on Wordpress meta_values and I'm using the MySQL statement below, which is not working. The SUM(returned.meta_value+staked.meta_value)-SUM(staked.meta_value))/SUM(staked.meta_value))*100) doesn't return any values.
Can someone help me with what's wrong with the expression below.
Thank you.
global $post;
$post_author = $post->post_author;
$post_status = 'publish';
$yield = $wpdb->get_var( $wpdb->prepare(
"
SELECT (ROUND((((SUM(returned.meta_value+staked.meta_value)-SUM(staked.meta_value))/SUM(staked.meta_value))*100),2),2)
FROM {$wpdb->posts} p
JOIN {$wpdb->users} u ON p.post_author = u.ID
LEFT JOIN {$wpdb->postmeta} staked
ON p.ID = staked.post_id AND staked.meta_key = 'staked'
LEFT JOIN {$wpdb->postmeta} returned
ON p.ID = returned.post_id AND returned.meta_key = 'returned'
WHERE p.post_author = %s AND p.post_status = %s
",
$post_author, $post_status
) );
Your query doesn't make sense. The select statement is using the aliases balance and bet. The from clause has the aliases pm and posts.
Do you mean this?
SELECT (ROUND((((SUM(pm.meta_value+pm.meta_value)-SUM(pm.meta_value))/SUM(pm.meta_value))*100),2),2)
FROM {$wpdb->postmeta} pm INNER JOIN
{$wpdb->posts} p
ON pm.post_id = p.ID
WHERE pm.meta_key = %s AND pm.meta_key1 = %s AND p.post_author = %s AND
Month(p.post_date) = MONTH(CURRENT_DATE) AND p.post_status = %s

Advanced MySQL query in Wordpress with JOIN

I am new to MySQL and I am trying to write a quite advanced query. But hey! Learning by doing & Stackoverflow!
I could probably get all data in a less advanced query/queries and sort the data using PHP. But I figure it could be done directly in the query.
Below is my code. Please ask question if you don't understand and I will try to explain better. And please help me correct the code if you find any errors.
The code will be used to display different fields on my wordpress pages. Different fildes will have different categories eg. sidebar-blog, sidebar-page, highlight-blog, highlight-page. It will work almost like regular posts.
Here is the database structure of Wordpress:
http://codex.wordpress.org/images/9/9e/WP3.0-ERD.png
http://codex.wordpress.org/Database_Description
Questions:
How am I supose to join the tables: wp_posts, wp_term_relationships, wp_term_taxonomy, wp_terms AND wp_postmeta?
Is it good practice to make advanced queries or should you use PHP to handle if/else functions?
<?php
$id = $post->ID; // Gets the ID of current page
$query = "
SELECT wp_posts.post_content, wp_posts.ID, wp_terms.slug # Data from two different tables
FROM wp_posts
# Cant figure out how to join the tables
INNER JOIN wp_postmeta
ON wp_posts.ID = wp_postmeta.post_id
INNER JOIN wp_term_relationships
ON wp_posts.ID = wp_term_relationships.object_id
INNER JOIN wp_term_taxonomy
ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
INNER JOIN wp_terms
ON wp_term_taxonomy.term_id = wp_terms.term_id
WHERE
wp_posts.post_type = 'my-own-post-type' # Only get specific post-type
AND
wp_posts.post_status = 'publish' # Only get published posts
AND
# START - Only get categories specified here
(
wp_terms.slug = 'category-1'
OR
wp_terms.slug = 'category-2'
OR
wp_terms.slug = 'category-3'
OR
wp_terms.slug = 'category-4'
OR
wp_terms.slug = 'category-5'
)
# END - Only get categories specified here
AND
# I want to be able to include or exclude specific pages, even if category is the right one
# Exlude = Don't get data if current page ID is found (ID of current page will be checked in meta_value using %,$id,%)
# Include means: If include value is specifyed, then get data ONLY if ID is found (ID of current page will be checked in meta_value using %,$id,%)
# If not exclude and include are set, it should get the data
# START - Include exclude
(
# exclude is set, so check if page id match anywhere. If it IS found it it should not get the data, otherwise YES
wp_postmeta.meta_key = 'exclude' AND wp_postmeta.meta_value <> '%,$id,%'
OR
# include is set, so check if page id match anywhere. If it IS NOT found it it should not get the data, otherwise YES
wp_postmeta.meta_key = 'include' AND wp_postmeta.meta_value = '%,$id,%'
OR
# If exclude and include aren't set it should get the data
wp_postmeta.meta_key <> 'exkludera' AND wp_postmeta.meta_key <> 'include'
)
# END - Include exclude
";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
// collect all fields in category 1 (eg. sidebar)
if($row['slug'] == 'category-1'){
$all_fields_from_category_1 = $all_fields_from_category_1.'
<div id="field-sidebar">
'.$row['post_content'].'
</div>
';}
// collect all fields in category 1 (eg. highlight)
if($row['slug'] == 'category-2'){
$all_fields_from_category_2 = $all_fields_from_category_2.'
<div id="field-highlight">
'.$row['post_content'].'
</div>
';}
} // end while
// sidebar
echo '<div id="container-sidebar">'.
$all_fields_from_category_1.
'</div>';
// highlight
echo '<div id="container-highlight">'.
$all_fields_from_category_1.
'</div>';
?>
New version, new question:
SELECT DISTINCT wp_posts.post_content, wp_posts.ID, wp_terms.slug
FROM wp_posts
JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id
JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
JOIN wp_terms ON wp_term_taxonomy.term_id = wp_terms.term_id
WHERE wp_posts.post_type = 'my-own-fields'
AND wp_posts.post_status = 'publish'
AND wp_terms.slug IN
('field1', 'field2', 'field3', 'field4', 'field5', 'field6')
AND
(
wp_postmeta.meta_key = 'exlude' AND wp_postmeta.meta_value <> '$id'
OR wp_postmeta.meta_key = 'include' AND wp_postmeta.meta_value = '$id'
OR wp_postmeta.meta_key <> 'exlude' AND wp_postmeta.meta_value <> 'include' #LAST ROW
)
The "#LAST ROW" gives me back data even if exclude or include is set. Thats becauce it matches two other rows in the table wp_postmeta.
Example of table wp_postmeta:
meta_id post_id meta_key meta_value
1 30 include 18
2 30 _edit_lock 1322225789:1
3 30 _edit_last 1
If meta_key inklude or exclude cant be found for the post_id I want to return the data...
Example senario when I want to return the data:
meta_id post_id meta_key meta_value
2 30 _edit_lock 1322225789:1
3 30 _edit_last 1
Any idea of how I can solve this?
A statement that say: If no rows with exclude or include was found for current id. Return the data.
More examples:
The post_id is 30.
If senario is:
meta_id post_id meta_key meta_value
1 30 include 18
2 30 _edit_lock 1322225789:1
3 30 _edit_last 1
Then I want to get back wp_posts.post_content, wp_posts.ID and wp_terms.slug AS LONG AS $id IS 18.
If senario is:
meta_id post_id meta_key meta_value
1 30 exclude 18
2 30 _edit_lock 1322225789:1
3 30 _edit_last 1
Then I want to get back wp_posts.post_content, wp_posts.ID and wp_terms.slug AS LONG AS $id IS NOT 18.
If senario is:
meta_id post_id meta_key meta_value
2 30 _edit_lock 1322225789:1
3 30 _edit_last 1
Then I want to get back wp_posts.post_content, wp_posts.ID and wp_terms.slug.
Your joins are done correctly. When removing your comments and extra lines, you will see that the query is not really that complicated. The only real change I made was to change your list of OR for wp_terms.slug to an IN statement. This was only done to reduce code duplication, clean up the look, not to change functionality.
SELECT p.post_content, p.ID, t.slug
FROM wp_posts AS p
INNER JOIN wp_postmeta AS pm ON p.ID = pm.post_id
INNER JOIN wp_term_relationships AS tr ON p.ID = tr.object_id
INNER JOIN wp_term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN wp_terms AS t ON tt.term_id = t.term_id
WHERE p.post_type = 'my-own-post-type'
AND p.post_status = 'publish'
AND t.slug IN
('category-1', 'category-2', 'category-3', 'category-4', 'category-5')
AND
(
( pm.meta_key = 'exclude' AND pm.meta_value <> '$id' )
OR ( pm.meta_key = 'include' AND pm.meta_value = '$id' )
OR ( pm.meta_key <> 'exclude' AND pm.meta_value <> 'include' )
)