I want a mysql query to get all my orderd on pending status. Somebody know how to do that.
I'm using woocommerce ina wordpress website, but I just want the mysql query not with wordpress functions.
Thanks
You really should use the built-in database functions since that complies with the Wordpress coding standards.
You could look at the WC_Query class.
Or you could try something like the below. You will need to change the meta key to whatever the meta key is they are using. Also the meta value might be different than pending and the post_type may be different than 'shop_order'.
$pending = new WP_Query(
array(
'post_type' => array('shop_order'),
'meta_query' => array(
array(
'key' => 'status',
'value' => array('pending'),
)
)
)
);
Here is one example on how to do a meta query of woocommerce orders.
OK guys, it is a weird issue, but I fixed it by simply using a custom query. Somehow adding 'post_status' => 'wc-pending' doesn't actually change the query, but if I use pending, the query changes.
So what I did was using that custom query and modify pending to wc-pending.
Well I spent the weekend working on the query and here is the result:
select
nombre,apellido,direccion,direccion2,codigo,poblacion,provincia,correo, telefono
from (
select
(select meta_value from wp_postmeta pm1 where p.ID = pm1.post_id and meta_key = "_billing_first_name") as nombre,
(select meta_value from wp_postmeta pm1 where p.ID = pm1.post_id and meta_key = "_billing_last_name") as apellido,
(select meta_value from wp_postmeta pm1 where p.ID = pm1.post_id and meta_key = "_billing_address_1") as direccion,
(select meta_value from wp_postmeta pm1 where p.ID = pm1.post_id and meta_key = "_billing_address_2") as direccion2,
(select meta_value from wp_postmeta pm1 where p.ID = pm1.post_id and meta_key = "_customer_user") as codigo,
(select meta_value from wp_postmeta pm1 where p.ID = pm1.post_id and meta_key = "_billing_city") as poblacion,
(select meta_value from wp_postmeta pm1 where p.ID = pm1.post_id and meta_key = "_billing_state") as provincia,
(select meta_value from wp_postmeta pm1 where p.ID = pm1.post_id and meta_key = "_billing_email") as correo,
(select meta_value from wp_postmeta pm1 where p.ID = pm1.post_id and meta_key = "_billing_phone") as telefono
from
wp_posts AS p
WHERE post_type = "shop_order" and id in (SELECT object_id
FROM wp_posts
LEFT OUTER JOIN wp_term_relationships ON wp_posts.ID=wp_term_relationships.object_id
WHERE post_type = "shop_order"
AND term_taxonomy_id=9
)
) A
Related
I tried to make a complex query to WordPress database but it did not work, so I ask for help.
First I need to get the meta value from one table, and then using it to get the ID from another table.
For this I use the following code:
global $wpdb;
$event_ID = 306;
$meta_value = $wpdb->get_var( "SELECT meta_value FROM wp_postmeta
WHERE meta_key = 'driver_type' AND post_id = '{$event_ID}'" );
$event_type = $wpdb->get_var( "SELECT post_title FROM wp_posts
WHERE ID = '{$meta_value}'" );
I would like to optimize the query. Tried to make here such request but it returns to me 1 instead of the necessary value:
$complex = $wpdb->query( "SELECT t2.post_title, t1.meta_value FROM wp_postmeta AS t1
INNER JOIN wp_posts AS t2 ON t1.meta_value = t2.ID
WHERE meta_key = 'driver_type' AND post_id = '{$event_ID}' GROUP BY t1.post_id" );
Please tell me how to make the correct complex query in my case.
You should be able to just chain together your two queries:
SELECT post_title
FROM wp_posts
WHERE ID = (SELECT meta_value FROM wp_postmeta
WHERE meta_key = 'driver_type' AND post_id = '{$event_ID}');
Or, you could try doing a join:
SELECT p.post_title
FROM wp_posts p
INNER JOIN wp_postmeta m
ON p.ID = m.meta_value
WHERE
m.meta_key = 'driver_type' AND post_id = '{$event_ID}';
I have 2 meta keys which i want attached to particular post type in a single instance, and i created the sql query
SELECT DISTINCT p.ID, p.post_title, pmo.meta_value, pmj.meta_value
FROM wp_posts AS p
RIGHT JOIN wp_postmeta AS pmo ON p.ID = pmo.post_id
RIGHT JOIN wp_postmeta AS pmj ON p.ID = pmj.post_id
WHERE (pmo.meta_key = '_order_meta' AND pmj.meta_key = '_order_jobs')
AND p.post_type = '_order'
AND p.post_status = 'publish'
ORDER BY p.ID DESC LIMIT 10
But when i var dump the data.
it only shows the last 'meta_value', although i am fetching 2 meta_vales
I need to retrieve a unique set of meta values of a specific meta key. I found a function from Paul Chimoy which does nearly what I am looking for. I just need 1). a version with a secondary qualification, and 2). a version with a secondary and tertiary qualification in order for it to output what I need.
The function outputs all the meta values for meta_key 'trees' and 'states' but I need to be able to output, 1). all unique meta values for meta_key 'trees' where meta_key 'states' equals 'california' and 2). all unique values for meta_key 'trees' where meta_key 'states' equals 'california' and meta_key 'countries' equals 'usa'.
Trees
meta_key = trees
meta_value = pine, oak, sequoia
States
meta_key = states
meta_value = california, washington, florida
Countries
meta_key = countries
meta_value = usa, uk, ireland
Without success, I adjusted the function to include:
AND (pm.meta_key = 'states' AND pm.meta_value = 'california')
Here is the original function without my additional qualification:
function get_unique_post_meta_values( $key = 'trees', $type = 'post', $status = 'publish' ) {
global $wpdb;
if( empty( $key ) )
return;
$res = $wpdb->get_col( $wpdb->prepare( "
SELECT DISTINCT pm.meta_value FROM {$wpdb->postmeta} pm
LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
WHERE pm.meta_key = '%s'
AND p.post_status = '%s'
AND p.post_type = '%s'
", $key, $status, $type ) );
return $res;
}
My limited knowledge is not allowing me to properly adjust the function to my needs. I appreciate any help that can be provided to achieve my goals.
Thanks in advance.
I know this is old, and you probably are not looking anymore, but if someone else needs this, here is what needs to be done.
Right now your select statement is:
SELECT DISTINCT pm.meta_value FROM {$wpdb->postmeta} pm
LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
WHERE pm.meta_key = '%s'
AND p.post_status = '%s'
AND p.post_type = '%s'
That will select the meta value based on one key.
Instead you want to get the unique meta values of another key based on one key.
To do this you need to select the post, then select other distinct meta values based on your required key. This means you need to nest your SQL, and grab your post ID.
SELECT DISTINCT pm.meta_value FROM {$wpdb->postmeta} pm WHERE pm.post_id IN
(SELECT pm.post_id FROM {$wpdb->postmeta} pm
LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
WHERE pm.meta_key = '%s'
AND pm.meta_value = '%s'
AND p.post_status = '%s'
AND p.post_type = '%s')
AND pm.meta_key = '%s'
Your wrapping statement should look something like:
$res = $wpdb->get_col( $wpdb->prepare($sql, $searchkey, $searchvalue, $poststatus, $posttype, $findkey ) );
Assuming you feed in the SQL above, trees, pine, post, publish, states - you should retrieve a list of states where pine trees grow.
Hope this helps anyone who stumbles upon this!
I am trying to get data with SQL from my Wordpress database by a JOIN, but I do not get it working.
What I need:
Posts where the meta_key "gtp_product_dont_show" exists and where the meta_value not is "true".
And posts where the meta_key "gtp_product_dont_show" does not exists.
My Problem:
I now only get the results where posts have a meta_key "gtp_product_dont_show", but there are also posts which don't have this key, and I need these as well.
This is what I have now:
SELECT
ID, post_title
FROM
wp_posts p
JOIN wp_postmeta m ON
p.ID = m.post_id AND
m.meta_key = 'gtp_product_dont_show' AND
m.meta_value != 'true'
WHERE
post_type = 'products' AND
post_status = 'publish'
Output:
You need a left join:
SELECT ID, post_title
FROM wp_posts p LEFT JOIN
wp_postmeta m
ON p.ID = m.post_id AND
m.meta_key = 'gtp_product_dont_show'
WHERE (m.meta_value is null or m.meta_value <> 'true') and
post_type = 'products' AND
post_status = 'publish';
The left join looks for an appropriate key in the wp_postmeta table. The where clause then says "keep a record when there is no match or the value is not true" -- which I think is the logic you are looking for.
You're looking for this?
SELECT
ID, post_title
FROM
wp_posts p
WHERE
post_type = 'products' AND
post_status = 'publish' AND
not exists (
select 1
from wp_postmeta m
where
p.ID = m.post_id AND
m.meta_key = 'gtp_product_dont_show' AND
m.meta_value = 'true')
This will fetch all the rows from wp_posts, but leave out those where row is found from wp_postmeta where meta_key is gtp_product_dont_show and value is true.
You can use the OR operator to consider both conditions. Try this:
SELECT stuff
FROM myTable
JOIN otherTable ON
(m.meta_key = 'gtp_product_dont_show' AND m.meta_value != 'true') OR
(m.meta_key != 'gtp_product_dont_show')
...
Just a side note, I don't recommend storing booleans as strings like that. You should consider using a TINYINT() where boolean fields are stored as 0 for false, or 1 for true.
WordPress's wp_postmeta table has all the additional fields for a post but they are in rows so it's easy to add more.
However, now I want to query for all the fields of all the posts lets say, I obviously want those fields in a column and not a row.
This is my query that I am running
SELECT p.post_title,
m.meta_value,
m.meta_key
FROM wp_posts p
JOIN wp_postmeta m
ON p.id = m.post_id
WHERE p.id = 72697;
This will give me all the meta_values and their respective meta keys as columns. But I need the meta keys values as columns and meta values as rows
For example a meta_key could be additional_description and it's value could be What's up
So I need something like this
SELECT p.post_title, additional_description
FROM wp_posts p
JOIN wp_postmeta m
ON p.id = m.post_id
WHERE p.id = 72697;
I need it as a column. I also need all of the posts and not a specific one, but whenever I remove the where it just doesn't query (I have lots of posts, that could be an issue).
Here is some sample data and how I want the results to show up
wp_postmeta table
meta_key post_id meta_key meta_value
1 5 total_related 5
2 5 updated 0
3 5 cricket 1
4 8 total_related 8
5 8 updated 1
6 8 cricket 0
wp_post table
id post_title other things I dont care about
5 This is awesome
8 This is more awesome
wp_post id is related to post_id on wp_postmeta table
Result wanted
post_title total_related updated cricket
This is awesome 5 0 1
This is more awesome 8 1 0
What about something like this?
SELECT p.post_title, m1.meta_value as 'total_related', m2.meta_value as 'updated', m3.meta_value as 'cricket'
FROM wp_posts p
LEFT JOIN wp_postmeta m1
ON p.id = m1.post_id AND m1.meta_key = 'total_related'
LEFT JOIN wp_postmeta m2
ON p.id = m2.post_id AND m2.meta_key = 'updated'
LEFT JOIN wp_postmeta m3
ON p.id = m3.post_id AND m3.meta_key = 'cricket'
And since you aren't looking for a specific post you should be able to do this.
If you want to query specific post_types you can try something like this
SELECT p.post_title, m1.meta_value as 'total_related', m2.meta_value as 'updated', m3.meta_value as 'cricket'
FROM wp_posts p
LEFT JOIN wp_postmeta m1
ON p.id = m1.post_id AND m1.meta_key = 'total_related'
LEFT JOIN wp_postmeta m2
ON p.id = m2.post_id AND m2.meta_key = 'updated'
LEFT JOIN wp_postmeta m3
ON p.id = m3.post_id AND m3.meta_key = 'cricket'
WHERE p.post_type = 'my_custom_post_type';
Try that:
select post_title ,
MAX(CASE WHEN `meta_key`='total_related' THEN meta_value END)as 'total_related',
MAX(CASE WHEN `meta_key` = 'updated' THEN meta_value END) as 'updated' ,
MAX(CASE WHEN `meta_key` = 'cricket' THEN meta_value END) as 'cricket'
FROM wp_posts p
JOIN wp_postmeta m ON p.id = m.post_id
GROUP BY p.id
There are several approaches.
Here's an example of one way to get the specified result, using correlated subqueries in the SELECT list:
SELECT p.post_title
, ( SELECT m1.meta_value
FROM wp_post_metadata m1
WHERE m1.meta_key = 'total_related'
AND m1.post_id = p.id
ORDER BY m1.meta_key LIMIT 1
) AS `total_related`
, ( SELECT m2.meta_value
FROM wp_post_metadata m2
WHERE m2.meta_key = 'updated'
AND m2.post_id = p.id
ORDER BY m2.meta_key LIMIT 1
) AS `updated`
, ( SELECT m3.meta_value
FROM wp_post_metadata m3
WHERE m3.meta_key = 'cricket'
AND m3.post_id = p.id
ORDER BY m3.meta_key LIMIT 1
) AS `cricket`
FROM wp_posts p
WHERE p.id IN (5,8)
There are several other approaches, each with its own advantages and drawbacks.
There's a somewhat related question I referenced in a comment on the question. That question illustrates several approaches, but omits a correlated subquery approach.)
Here's how I did this dynamically - this procedure builds a SQL statement for every postmeta key for a given post type and then runs the "pivot" query for you:
This isn't the fastest query, and we use it only for migration and deep dives into data, but it does the job.
Note that this temporarily resets the max length of the concat function so you can build a large SQL statement:
CREATE PROCEDURE `wp_posts_pivot`(IN post_type_filter varchar(50))
BEGIN
/* allow longer concat */
declare max_len_original INT default 0;
set max_len_original = ##group_concat_max_len;
set ##group_concat_max_len=100000;
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT CONCAT('MAX(IF(pm.meta_key = ''',
meta_key,
''', pm.meta_value, NULL)) AS `',
meta_key,
'`'))
INTO #sql FROM
wp_posts p
INNER JOIN
wp_postmeta AS pm ON p.id = pm.post_id
WHERE
p.post_type = post_type_filter;
SET #sql = CONCAT('SELECT p.id
, p.post_title
, ', #sql, '
FROM wp_posts p
LEFT JOIN wp_postmeta AS pm
ON p.id = pm.post_id
where p.post_type=\'',post_type_filter,'\'
GROUP BY p.id, p.post_title');
/* reset the default concat */
set ##group_concat_max_len= max_len_original;
/*
select #sql;
*/
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
You can then call this with a simple call such as this one, which will select a single row for each 'page' post type along with all meta values:
call wp_posts_pivot('page');