Reputation: 403
Is there a way to display all post and sorted according to the value of a specific custom field in WP?
Example: I have a post Milk and it has custom fields
- product_brand = Bear Brand
- product_description = 200 g
- product_price = $20.00
- product_week = June 01 -07, 2014
I want to display all post according to its product week value(June 01-07, 2014 and June 08-14, 2014)
I have tried this codes below and dont know what to add to sort this products, newbie to wp
<?php
query_posts(array(
'meta_key' => 'product_week',
'meta_value' => 'June 01 - 07, 2014'
));
if ( have_posts() ) while ( have_posts() ) : the_post();
echo '<div class="col-md-3">';
the_post_thumbnail('post-thumbnail', array( 'class' => "img-responsive"));
echo '<span class="product-title">';
the_title();
echo '</span>';
echo '<span class="product-price">';
echo '</span>';
echo '</div>';
endwhile;
wp_reset_query();
?>
Upvotes: 0
Views: 44
Reputation: 1402
You don't say whether your code works but in the wrong order or if it doesn't work at all.
I am not a wordpress expert but that doesn't matter because your issue isn't really about Wordpress, it is more about databases and sorting in general.
For this to work you are going to need to give the product week a number value (hidden) as well as their descriptive value for display. Otherwise how do you sort them?
Alphabetically, December comes before June, April comes first.
However if use a number you can (obviously) then sort them easily and the best format to use is YYWW (Year + Week Number). A value of 1401 (2014 week 1) comes before 1447 (2014 week 47) which comes before 1501 which is before 1536 etc.
So you might end up with
product_brand = Bear Brand
product_description = 200 g
product_price = $20.00
product_week = June 01 -07, 2014
Product_week_id = 1427 (or whatever week number it is)
Then you can adjust your code to suit
Upvotes: 1
Reputation: 531
The data of datetime can only be stored in datetime format in mysql database you don't have a wordpress problem , it is a mysql problem
you can't store a data like july , 2012 blah blah
in mysql
you have to use the specified format
if you have stored the data in format then you have to convert these dates in numbers and search in mysql table of post_meta
see the strtotime in php
Upvotes: 1