in Wordpress, I've got a category called "Reviews" where I assign a vote (in a custom field called 'rate') to every single post I create in this category.
Now I'm trying to get the review with the highest rate in the last X months.
The rate ranges from 0 to 10, with one decimal number (ex. 5.3, 7.4, 9.1).
How can I do this? Use WP_Query or direct DB query? Thanks in advance.
You can use something like this to get the highest rating:
SELECT max(cast(meta_value as unsigned)) FROM wp_postmeta WHERE meta_key='your_meta_name'
But post meta data does not hold the date/time value for you. You need to think a way to insert date value as well.
Related
I am new to SugarCRM. I have a requirement to calculate a field value through studio. But, in one of the fields which comes in denominator can be 0. So, i want to modify the formula through ifElse, how to do that.
Example - If (field1>0){calcField/field1} else(calcField=0).
you can use calculated field , if else like this:
ifElse(equal($status,"Held"),1,0)
For more information check this link :
Calculated field in sugarcrm
Essentially I want these parts (below) grouped then the groups place in order of time, starting from the latest time being at the top of the list.
ID Parts Time
1 SMH_2010 08:59:18
2 JJK_0101 08:59:26
3 FTD_0002 08:59:24
4 JJK_0102 08:59:27
5 FTD_0001 08:59:22
6 SMH_2010 08:59:20
7 FTD_0003 08:59:25
So, the results would look like:
ID Parts Time
1 JJK_0101 08:59:26
2 JJK_0102 08:59:27
3 FTD_0001 08:59:22
4 FTD_0002 08:59:24
5 FTD_0003 08:59:25
6 SMH_2010 08:59:20
7 SMH_2010 08:59:18
Please, I would be grateful for any help.
What you are asking is not sorting in the traditional meaning. Your first attempt orders the result by time, and then by part if multiple timestamps occur at the same time.
What you want neither sorts the result in alphabetically by Parts name, nor ascending/descending on timestamp. What you are asking for can't be accomplished by the sort operation in SQL. Having the parts in sequence is not ordering.
I finally found a solution to this. Not my ideal solution but, never the less it works.
I added another field called max_date which by default is ‘now()’ as every new part is inserted.
I create a prefix from the current part being inserted, something like “SMH_” as a variable called $prefix = “SMH_”;
I have another query that directly follows the insert, which updates the max_date again, by ‘now()’ where the prefix is like $prefix.
UPDATE parts SET max_date = now() WHERE prefix LIKE '%$prefix%'
To display the results I use something along the line of :
SELECT * FROM parts ORDER BY parts.max_date DESC, parts.part ASC
I am needing to have a custom element that will look at a drop-down called month and conditionally insert the numeric month into the database. For example if the user selects October it would put the number 10 in the numeric_month field in the DB. The month drop-down is set to another field in the same database and unfortunately there is no way to combine them for our purposes. Any help would be appreciated. I am new to php coding.
Create a hidden field in your form for numeric_month
add a "Custom Code" action to the "On Submit" event before your "DB Save Action"
In the Custom Code action insert PHP code to set the numeric_month field to the appropriate value depending on the selected drop down value.
In ChronoForms v4 or v5, this will be something similar to this:
<?php
$form->data['numeric_month'] = date("n",strtotime($form->data['month']));
?>
This would set numeric_month to '7' for a month value of 'July'.
Use date("m",strtotime($form->data['month']) to set numeric_month to '07' instead.
For more information about the PHP code to convert a month name to a numberic string, see the answers to these questions:
convert month from name to number
PHP convert short month-name to month-number
So, I have a custom field type called "postorder." I added some php to the template page (see below) to call in the "postorder" for each of the posts, and to sort them in descending order. The problem I am having is that Wordpress is putting 10 after 1. So, the order of my posts is 1,10,2,3,4,5,6,7,8,9. I would like for 10 to come after 9, or find a different value to order my posts.
query_posts('showposts=1000&meta_key=postorder&orderby=meta_value&ASC&post_type='.portfolio);
WordPress is treating the values as string to sort, so 10 comes after 1, just change following
orderby=meta_value
to (values will be treated as number)
orderby=meta_value_num
When sorting by number, use meta_value_num instead of meta_value to make WordPress treat the value as a number instead of a string.
I have about 10,000 products in the product table. I want to retrieve one of those items and display it in a section of a web page which stays the same for that particular day. Something like "Product of the day".
For example, if today I get product_id 100, then all of the visitors should be viewing this product item for today. Tomorrow it may fetch any random valid primary key, say, 1289 and visitors get 1289 product all day tomorrow.
Any ideas/suggestions?
Thanks for your help.
SELECT id
FROM products
ORDER BY
RAND(UNIX_TIMESTAMP(CURRENT_DATE()))
LIMIT 1
Maybe you can store the id of the item of the day in a table in the database?
How about create a cache file and invalidate it at midnight?
The benefit of this is you don't make unnecessary calls to your DB as you're only checking the timestamp on the cache file - only once per day do you make DB requests to populate a new cache file.
You don't need a CRON job for this:
if(date_of_file(potd_cache_file) != today){
potd_cache_file = generate_from_db();
}
load_file(potd_cache_file);
This will mean only the first visitor of the day to your website will trigger the regeneration, and every subsequent visitor will have a fast loading cache file served to them.
The idea is pretty simple,
Set a table up call ProductOfTheDay with a product ID and a date field
On the product of the day page when a user visits check the date field
If it is todays date then show the product
If it is not then randonly pick a new product and save it to the field.
Its not that complex of an operation.
SELECT id
FROM products
ORDER BY (id + RAND(UNIX_TIMESTAMP(CURRENT_DATE()))) MOD some_reasonable_value
LIMIT 1
You can start random number generators with a seed value.
Make the seed value be the day (21st) + month(10) + year(2009) so today's seed is 2041.
You will get the same random number all day, and tomorrow a different one. This is more how it works in .net. The random function takes a max and min value (this is your min and max ID values) then an optional seed value and returns a number. For the same seed number you get the same random number generated. It's possible if you change the max and min this can affect the number generated. You would have to look up how php works.
total = SELECT COUNT(id) FROM products;
day_product = SELECT id FROM products WHERE id = (UNIX_TIMESTAMP(CURRENT_DATE()) MOD total) LIMIT 1;
See also this question.