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'
Related
I have the following query that bring the posts that contain a specific word in the title
SELECT posts_post.ID AS post_ID,
posts_post.post_date AS post_post_date,
CONCAT('',posts_post.post_title,'') AS post_title_with_link_to_post
FROM wp_posts AS posts_post
WHERE 1=1
AND posts_post.post_title LIKE '%HOTARAR%'
AND posts_post.post_type = 'post'
GROUP BY post_post_date
The problem now is that I need to bring the posts only from a specific category (tag slug for the category is hotarari-consiliu-local and has the ID 160), how could I modify the above query to bring posts only from a single blog posts category? Thanks!
Try the below query.
global $wpdb;
$make = $wpdb->get_results("
SELECT * FROM
$wpdb->posts
LEFT JOIN
$wpdb->term_relationships
ON
($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN
$wpdb->term_taxonomy
ON
($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE
$wpdb->posts.post_type = 'post'
AND
$wpdb->posts.post_title LIKE '%HOTARAR%'
AND
$wpdb->term_taxonomy.taxonomy = 'category'
AND
$wpdb->term_taxonomy.term_id = 160
ORDER BY
post_date DESC
");
Assuming Bhautik is on the right path, then something like this should work (I don't know where 'hotarari-consiliu-local' belongs in this).
SELECT p.ID post_ID
, p.post_date post_post_date
, CONCAT('',p.post_title,'') post_title_with_link_to_post
FROM wp_posts p
JOIN wp_term_relationships pt
ON pt.object_id = p.ID
JOIN wp_term_taxonomy t
ON t.term_taxonomy_id = pt.term_taxonomy_id
WHERE p.post_type = 'post'
AND p.post_title LIKE '%HOTARAR%'
AND t.taxonomy = 'category'
AND t.term_id = 160
ORDER
BY post_date DESC
so I have this SQL query:
SELECT
p.ID
FROM
`cdlr_posts` p,
cdlr_postmeta pm
WHERE
pm.post_id=p.ID AND
`post_type` = 'shop_order' AND
pm.meta_key = '_statusCDLR' AND
pm.meta_value <> 1
group by
p.ID
What I need is to show all the IDS if they match with those conditions, but I will also like to show the ones that do not contain the "_statusCDLR" meta_key I tried something like this with no luck:
WHERE
pm.post_id=p.ID AND
`post_type` = 'shop_order' AND
(pm.meta_key = '_statusCDLR' AND pm.meta_value <> 1 OR pm.meta_key <> '_statusCDLR')
group by
Any help will be appreciated to achieve what I need.
I understand that your requirement is to select the id of posts that :
either have a corresponding record in cdlr_post_meta with meta_key = 'statusCDLR' and meta_value <> 1
or do not have a record cdlr_post_meta with meta_key = 'statusCDLR'
A strategy to achieve this is to use a LEFT JOIN to search for a record in cdlr_post_meta with meta_key = 'statusCDLR', and then implement the rest of the logic in the WHERE clause (if there is no corresponding record, the columns of pm are all NULL).
SELECT p.ID
FROM cdlr_posts p
LEFT JOIN cdlr_postmeta pm
ON pm.post_id = p.ID AND pm.meta_key = '_statusCDLR'
WHERE
p.post_type = 'shop_order'
AND ( pm.post_id IS NULL OR pm.meta_value <> 1 )
GROUP BY p.ID
PS - General remarks regarding your sql :
When mixing ORs and ANDs, you need to surround the test expressions within parentheses to avoid running into prescedence issues (AND has higher precedence than OR).
you should use explicit JOINs instead of implicit ones.
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?
I've developed a query which selects events from Wordpress. I am using the where clause to select where the meta_value of the meta_key eventstartdate is after today.
The issue I'm having now is, I also want filter on a second meta_value that being from the meta_key '_VenueCity'.
I have tried aliasing the wp_postmeta table and doing a where on the meta_key but I think I'm missing a join.
This is the code that works without my additional code to get it to work. Can any one advise on how I get this to work?
SELECT
`wp_posts`.`ID` AS `EventID`,
`wp_posts`.`post_parent` AS `SeriesID`,
`wp_posts`.`post_title` AS `EventTitle`,
`wp_posts`.`post_content` AS `EventDescription`,
`wp_posts`.`post_excerpt` AS `EventSummary`,
`wp_posts`.`post_name` AS `EventSlug`,
min(`wp_postmeta`.`meta_value`) AS `EventStartDate`,
max(`tribe_event_end_date`.`meta_value`) AS `EventEndDate`,
`wp_posts`.`guid` AS `GUID`
FROM ((`wp_posts`
JOIN `wp_postmeta` ON
(
(`wp_posts`.`ID` = `wp_postmeta`.`post_id`)
))
LEFT JOIN `wp_postmeta` `tribe_event_end_date` ON
(
(
(`wp_posts`.`ID` = `tribe_event_end_date`.`post_id`) AND
(`tribe_event_end_date`.`meta_key` = '_EventEndDate')
)
))
WHERE
(
(`wp_postmeta`.`meta_key` = '_EventStartDate') AND
(`wp_posts`.`post_type` = 'tribe_events') AND
(`wp_posts`.`post_status` = 'publish') AND
(`tribe_event_end_date`.`meta_value` >= CURDATE())
)
GROUP BY
`wp_posts`.`ID`
ORDER BY
`EventStartDate`,
`wp_posts`.`post_date`;
I am not going to write your query for you but I will give an example of how to get multiple postmeta values. The power will be in the where clause to get the right values.
You should also consider what joins you want to use.
SELECT
p.post_title,
pm1.meta_value,
pm2.meta_value
FROM wp_posts as p
INNER JOIN wp_postmeta as pm1
ON p.ID = pm1.post_id
INNER JOIN wp_postmeta as pm2
ON p.ID = pm2.post_id
WHERE
pm1.meta_key = '_my_postmeta_field1'
AND
pm2.meta_key <> '_not_this_field'
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