Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want list all the upcoming and past events(custom posts) in chronological order. I have saved data by meta key start_time,duration ,

I try to use sql ,but can not get there!

Thanks for any help in advance!

share|improve this question
1  
How is your date meta stored (what format)? If it is a Unix timestamp, you could set the orderby parameter of your query to meta_value_num. –  Mateusz Hajdziony Jan 9 '13 at 3:51
    
time stamp,well I try this: SELECT (SELECT ABS(m1.meta_value-UNIX_TIMESTAMP(NOW())) from wp_postmeta AS m1 WHERE m1.meta_key='st_time' AND m1.post_id=p.ID) AS dis FROM wp_posts AS p, wp_postmeta AS m WHERE p.ID = m.post_id ORDER BY dis,the query_post() may not have so much! –  liying Jan 9 '13 at 6:41
add comment

1 Answer

Try to use WP_Query instead of using the SQL. You can get the reference here.

Simple example would be like:

$args = array(
   'post_type' => 'my_custom_post_type',
   'meta_key' => 'date',
   'orderby' => 'meta_value_num',
   'order' => 'ASC',
   'meta_query' => array(
       array(
           'key' => 'event_date',
           'value' => date()
       )
   )
 );
 $query = new WP_Query($args);

and use a simple while loop to iterate through the results.

See this for more info on meta query parameters.

share|improve this answer
    
Well,I know this useful WP_Query,but the main point is I want to sort by the difference between meta_value and another object(like now time)! –  liying Jan 10 '13 at 2:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.