This question already has answers here:
Get orders total purchases amount for the day in Woocommerce
(2 answers)
Closed 4 years ago.
Is there anyway to get WooCommerce total sales dollar amount, I'm using this plugin and it's shortcode for this purpose but I'm looking for a way to get the today's total revenue amount (gross sales) without using a plugin. I'm gonna use that code inside another plugin that I'm developing.
Try this below code
$Select_Order_Details = $wpdb->get_results( "SELECT SUM(meta.meta_value) AS total_sales, COUNT(posts.ID) AS total_orders FROM wp_posts AS posts
LEFT JOIN wp_postmeta AS meta ON posts.ID = meta.post_id
WHERE meta.meta_key = '_order_total'
AND posts.post_type = 'shop_order'
AND posts.post_status IN ( '" . implode( "','", array( 'wc-completed', 'wc-processing', 'wc-on-hold' ) ) . "' )");
$Select_Order_Details = json_decode(json_encode($Select_Order_Details),true);
print_r($Select_Order_Details);exit;
Related
I have this category which is "community-posts" I don't want it to appear on my homepage loop so I added this to my query
<?php query_posts(array('showposts' => 4,'category__not_in' => $id_communityposts,));?>
This is working fine with me but some "community-posts" I want them to be featured on the homepage loop. (exception)
so I want to only exclude the posts that has one category as "community-posts" if it has this category and more its shows normally.
First thing do not use query_posts - it should never be used as it alter the main query. Use get_posts instead - it's much safer and perform the same task.
To answer your question, let's first imagine how the query would look in SQL (assuming your $id_communityposts is equal to 2) :
SELECT DISTINCT wp_posts.*
FROM wp_posts, wp_postmeta
LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
WHERE
wp_posts.ID = wp_postmeta.post_id AND
(
(wp_term_taxonomy.taxonomy = 'category' AND wp_term_taxonomy.term_id NOT IN(2))
OR
(wp_postmeta.meta_key = 'featured' AND wp_postmeta.meta_value = 1)
)
ORDER BY wp_posts.post_date DESC
LIMIT 4
So we query the post, post meta and taxonomy tables and make two possible conditions:
The category ID is not 2, OR
The featured meta key of the post is set to 1 (change this to whatever key / value depending of how you store the "featured" information).
For that kind of specific cases, get_posts isn't really good to play with - querying the DB with WPDB will give you much more flexibility.
$posts = $wpdb->get_results(
"SELECT DISTINCT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
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.ID = $wpdb->postmeta.post_id AND
(
($wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id NOT IN(2))
OR
($wpdb->postmeta.meta_key = 'featured' AND $wpdb->postmeta.meta_value = 1)
)
ORDER BY $wpdb->posts.post_date DESC
LIMIT 4"
);
Let me know if you run into any issue as it is an untested query.
If I understood the question correctly , The simplest solution, not involving complicated SQL would be something along the lines of :
// NOT TESTED !
if ( count(get_the_category()) > 1 ) { // this means there are more than single category
// show the desired posts
} else {
// dont show
}
read get_the_category() in codex
Along the same logic lines you could also use wp_get_post_categories
I'm having a little trouble coming up with the correct SQL for a Wordpress query.
What I have are some posts that contain a custom field called 'date'. I'd like to fetch ALL posts (both those containing this custom field plus those that don't) and sort them by date AND post_date. date will take precedence over post_date, i.e., if there's not a date specified, it'll use the post_date value instead.
What I have so far:
$query = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'date'
AND wposts.post_type = 'post'
ORDER BY wpostmeta.meta_value, posts.post_date ASC
";
Thx
a client want to show a total of the amount of products they have in their shop, I have used this code
$numposts = (int) $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'product' AND post_status = 'publish'");
Which works fine, however it shows the total number of published products and I want it to only show where stock is at 1 or greater, so basically it only shows the total number of products that are in actually in stock
Try joining to the post_meta table using the _stock_status meta_key where the meta_value is 'instock'. Caching the data is recommended since you don't want to run this on every request but will need to balance an appropriate amount of time to cache the data (since sales within the cache period won't be reflected in the total number of instock items). Only works if you are using a cache (which is highly recommended with WooCommerce due to the number of queries).
global $wpdb;
// cache key
$key = 'in_stock_products';
// we get back 'false' if there is nothing in the cache, otherwise 0+
$in_stock_products = wp_cache_get( $key );
// run the query if we didn't get it from the cache
if ( false === $in_stock_products ){
// create the SQL query (HEREDOC format)
$sql_query = <<<SQL
SELECT COUNT(p.ID) AS in_stock_products
FROM {$wpdb->posts} p
JOIN {$wpdb->postmeta} pm
ON p.ID = pm.post_id
AND pm.meta_key = '_stock_status'
AND pm.meta_value = 'instock'
WHERE p.post_type = 'product' AND p.post_status = 'publish'
SQL;
// query the database
$in_stock_products = (int) $wpdb->get_var( $sql_query );
// cache the results, choosing an appropriate amount of time to cache results
$cache_ttl = 0; // 0 is "as long as possible", otherwise cache time in seconds
wp_cache_add( $key, $in_stock_products, $cache_ttl ); // cache as long as possible
}
// $in_stock_products now has the value either from the cache or the database query.
echo "There are $in_stock_products in stock";
I have a table that saves the ratings for each post, the table looks like this http://d.pr/YIxL it saves the ID of the post and the rating, I need to query the posts based on that table ASC or DSC.
I hope that the answer is clear.
This was the query that did the trick
$wp_query->request = "
SELECT $wpdb->posts.* FROM $wpdb->posts
Left join $ratings_table
ON $wpdb->posts.ID = $ratings_table.ID
Where
$wpdb->posts.post_type = 'games' AND $wpdb->posts.post_status = 'publish'
ORDER BY $ratings_table.points DESC
LIMIT $ppp OFFSET $offset";
$pageposts = $wpdb->get_results($wp_query->request, OBJECT);
I finally got some code made for to output this years total published posts.
get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND `post_date` > '" . date("Y") . "-01-01 00:00:00'");
if (0 < $numposts) $numposts = number_format($numposts); ?>
I'm looking at the WP time paramters to try and make it also do day then week then month.
http://codex.wordpress.org/Function_Reference/query_posts#Time_Parameters
Can anyone advise me on what to do? Thanks
I think this thread should help you along.
This Query will give you the number of posts by year and month.
SELECT YEAR(post_date) as yer, MONTH(post_date) as mnth, COUNT(post_date) as num
FROM your_table
WHERE post_status = 'publish'
GROUP BY YEAR(post_date), MONTH(post_date)