Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Add timestamp to Insert/Update meta value query. WordPress

  • REFUNDED

I use a custom function that grabs the product information of the shopping cart, in which the product ID of the shopping cart is the same as the Post Id.

It adds a meta_key 'sales_count' to the wp_postmeta.
The meta_value is 1 after a sale, if there is another sale it updates to 2 etc.

I attach an image to show the result of 10 sales.
[[LINK href="http://i.imgur.com/ljQzK32.png"]]http://i.imgur.com/ljQzK32.png[[/LINK]]

In the image, on the last line there is also a wpfp_favorites meta, which i use in another page to sort by most favorited post via this query:


query_posts(array(
'showposts'=>32,'more' => $more = 0,
'orderby' => 'meta_value_num',
'meta_key' => 'wpfp_favorites',
'order' => 'DESC',
'paged' => $paged
));?>



My request is to have the same query to sort post by the number of sales_count AND with a date frame. Today, this week, this month.

For example:

query_posts(array(
'showposts'=>32,'more' => $more = 0,
'orderby' => 'TODAY',
'meta_key' => 'sales_count',
'order' => 'DESC',
'paged' => $paged
));?>



and:


query_posts(array(
'showposts'=>32,'more' => $more = 0,
'orderby' => 'THIS WEEK',
'meta_key' => 'sales_count',
'order' => 'DESC',
'paged' => $paged
));?>



and

query_posts(array(
'showposts'=>32,'more' => $more = 0,
'orderby' => 'THIS MONTH',
'meta_key' => 'sales_count',
'order' => 'DESC',
'paged' => $paged
));?>



Here is the code responsible for writing the meta sales_count. I hope someone knows how to add a timeframe to this function. If so, please modify it so i can paste and test.

Pastebin link: http://pastebin.com/ZAT1mN6Y


// START Custom code: log meta data
if(is_object($order) && $order->viewed == 0)
{
// init:
$metaKey = "sales_count" ;
$items = $order->getItems() ;

$isTesting = 0 ;

foreach ( $items as $item )
{
// fetch data:
$productId = $item->product_id ;
$quantity = $item->quantity ;

// product id is the pKey in the product table... we need the actual item id, which we manually force to be the post id
$tableName = Cart66Common::getTablePrefix() . "products" ;
$query = "SELECT item_number FROM $tableName WHERE id='$productId'" ;
$itemId = $wpdb->get_var ( $query ) ;

if ( $productId )
{
$numProducts = get_post_meta ( $productId, $metaKey, true ) ;

if ( ! $numProducts || $numProducts < 1 )
{
// insert
$newNumProducts = $quantity ;
}
else
{
// update
$newNumProducts = $numProducts + $quantity ;
}

update_post_meta ( $productId, $metaKey, $newNumProducts ) ;

if ( $isTesting > 0 )
{
print "<p>updated ($productId) $metaKey: $newNumProducts</p>\n" ;
// test meta:
$numProducts = get_post_meta ( $productId, $metaKey, true ) ;
print "<p>current meta [$numProducts]</p>\n" ;
}
}

Answers (1)

2013-09-06

Hariprasad Vijayan answers:

Hello,

In Wordpress, one possible value for order by is "date". (Like 'orderby' => 'date'). And 'orderby' => 'TODAY' or 'orderby' => 'THIS WEEK' ... is not possible. Do you wan't to filter posts based on date range?

Please let me know.


monitor comments:

No order by date will just have either oldest or newest. The principle is exactly the same as a View counts plugin, it counts the number of views, and increase the value, and it is saved to that post's meta.

In my code, the sales_count is saved, just as it would for a view, the only thing missing is the time stamp, as it is with a post view plugin.


monitor comments:

For example, I use this post views plugin [[LINK href="http://wordpress.org/plugins/post-views/"]]http://wordpress.org/plugins/post-views/[[/LINK]]

It has an option to view most viewed daily, weekly, monthly, yearly, and total.


Hariprasad Vijayan comments:

Let me check that plugin.

Can you show your site url. Where you want this


monitor comments:

There is no url, just the template call as showed above, and then the function code that writes the sales_count meta