admin管理员组

文章数量:1122846

We have "events" that are specific dates, so the page is set to put the events based on the day of the event first - then they should be sorted by the time the event takes place.

The dates are fine -- all the proper events are under the proper dates.

But the times are not consistent. About 80% of them are correct but every once in a while one falls out of place....so I may see something like this: (DAY 2 shows the issue)

DAY 1
 8:00am - 10:00am event 1
10:15am - 12:00pm event 2
 1:15pm -  2:00pm event 3
 3:15pm -  5:00pm event 4

DAY 2
 8:00am - 10:00am event 1
 3:15pm -  5:00pm event 4
10:15am - 12:00pm event 2
 1:15pm -  2:00pm event 3

This is the code that handles this area - can anyone offer advice on how to get the TIMES in ascending order as well?

echo '<div class="page hentry entry"><div class="entry"><ul class="conference_list">';
$year_to_show = '2016';

$args = array(
    'posts_per_page' => -1, 
    'meta_key'       => 'date', 
    'order'          => 'ASC', 
    'orderby'        => 'meta_value',
    'post_type'      => 'proposal'
);
$myQuery = new WP_Query( $args );

$date = CFS()->get( 'date' ); 

if ( $myQuery->have_posts() ) : while ( $myQuery->have_posts() ) : $myQuery->the_post();
    if ( date( 'Y', strtotime( CFS()->get( 'date' ) ) ) == $year_to_show ) {
        if ( $date != CFS()->get( 'date' ) ) {
            $date = CFS()->get( 'date' );
            echo '<div class="date_title">';
            echo date( 'F j, Y', strtotime( CFS()->get( 'date' ) ) );
            echo '</div>';
        } ?>
        <li><span><?php echo genesis_get_custom_field( 'start_time' ); ?>  - <?php 
            echo genesis_get_custom_field( 'end_time' );?>   </span>

We have "events" that are specific dates, so the page is set to put the events based on the day of the event first - then they should be sorted by the time the event takes place.

The dates are fine -- all the proper events are under the proper dates.

But the times are not consistent. About 80% of them are correct but every once in a while one falls out of place....so I may see something like this: (DAY 2 shows the issue)

DAY 1
 8:00am - 10:00am event 1
10:15am - 12:00pm event 2
 1:15pm -  2:00pm event 3
 3:15pm -  5:00pm event 4

DAY 2
 8:00am - 10:00am event 1
 3:15pm -  5:00pm event 4
10:15am - 12:00pm event 2
 1:15pm -  2:00pm event 3

This is the code that handles this area - can anyone offer advice on how to get the TIMES in ascending order as well?

echo '<div class="page hentry entry"><div class="entry"><ul class="conference_list">';
$year_to_show = '2016';

$args = array(
    'posts_per_page' => -1, 
    'meta_key'       => 'date', 
    'order'          => 'ASC', 
    'orderby'        => 'meta_value',
    'post_type'      => 'proposal'
);
$myQuery = new WP_Query( $args );

$date = CFS()->get( 'date' ); 

if ( $myQuery->have_posts() ) : while ( $myQuery->have_posts() ) : $myQuery->the_post();
    if ( date( 'Y', strtotime( CFS()->get( 'date' ) ) ) == $year_to_show ) {
        if ( $date != CFS()->get( 'date' ) ) {
            $date = CFS()->get( 'date' );
            echo '<div class="date_title">';
            echo date( 'F j, Y', strtotime( CFS()->get( 'date' ) ) );
            echo '</div>';
        } ?>
        <li><span><?php echo genesis_get_custom_field( 'start_time' ); ?>  - <?php 
            echo genesis_get_custom_field( 'end_time' );?>   </span>
Share Improve this question edited Dec 10, 2015 at 20:21 Gabriel 2,24810 gold badges22 silver badges24 bronze badges asked Dec 10, 2015 at 19:14 Jill CJill C 11 silver badge2 bronze badges 5
  • 3 Why not store these as real dates in your database with a timestamp that can be queried? You shouldn't have to sort twice for something like this. I think your issue is you are trying to query the time that the post was created vs the "event time". – JediTricks007 Commented Dec 10, 2015 at 20:53
  • The dates themselves are not the problem, those are sorting fine. It is the times that are not showing in the proper order --- those are handled via CFS with a text field --- and that is where I think the problem is. I have tried an array with a meta_value_num and that does not work either though. – Jill C Commented Dec 10, 2015 at 21:34
  • They are in fact the problem if you can't run queries on them. They are not proper date stamps. It sounds like your not storing data properly. You should just be able to have a timestamp column that queries the date and time. Why complicate things? Fix your data instead. – JediTricks007 Commented Dec 10, 2015 at 23:44
  • Thanks Jedi.....sadly that is a bit over my head. I am using custom field suite -- this is a field that the web owner has to input the times into. There is no numerical value option in CFS to be able to time stamp that I am aware of. I am still researching and trying to figure out how to best handle this being it is a live site. – Jill C Commented Dec 10, 2015 at 23:58
  • 2 You will have to store dates as single values as Ymd His format or unix timestamp format. Any other format will not sort properly. This is unfortunately how custom fields work. There are a way to solve your issue, but it is really expensive and will slow things down, something that you also would not want. Best solution is to rethink how your dates are stored – Pieter Goosen Commented Dec 11, 2015 at 4:33
Add a comment  | 

1 Answer 1

Reset to default 0

You can order by multiple things see more on https://make.wordpress.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/:

$args = array(
    'meta_key' => 'start_time',
    'orderby' => array('date' => 'ASC', 'meta_value' => 'ASC)
);

本文标签: custom post typesSort by datethen by time