Make events display in right order

Hi there!

I am a total newcomer to PHP. I have a wordpress site displaying events (posts) that are generated with with custom fields. I have not made the site but I’ve done some changes lately. The front page should show only upcoming events. The event nearest in time should show up first. It has been working well for long, but at some point it stopped working. Now I can only display events in DESC order and when I change to ASC the if-statement returns that there are no upcoming events. (For you who know wordpress: if I make an event “sticky” it will show up on top, but thats a temporary work around that only work for one post)

I have tried to deactivate plugins, with no luck. There is a chance it has to do with some major update of wordpress. Now I’m turning to you guys to see if you can see some obvious fault in the code. It’s probably not good code at all but it has been working well at least.

The if statement to display only upcoming events take the date and use strtotime, then adds a day to make sure the event shows the entire day it’s active. I have a feeling it has to do with how this conversion match the dates on the posts. But it’s weird that it display all events at DESC and none if I change to ASC… well…

Thanks a lot for any kind of help.

<h2>Upcoming events</h2>
                <hr>
                <?php
                $args = array(
                    'numberposts' => -1, 
                    'meta_key' => 'start_date',
                    'orderby' => 'meta_value',
                    'order' => 'DESC', 
                    'meta_query' =>array(
                        array(
                    'key' => 'show_on_front_page',
                    'value' => '1'
                        )
                        
                    )
                );
                
                
                $eventPosts = query_posts($args);
                //print_r($eventPosts);
                
                $didOutputEventPost = false; // display message about no upcoming events
                
				foreach ($eventPosts as $post) {
                    
                    $meta = get_fields($post->ID);
                    $date = $meta['start_date'];
                    $year = substr($date, 0, 4);
                    $month = ltrim(substr($date, 4, 2), '0');
                    $day = ltrim(substr($date, 6, 2), '0');

                    ?> 
             
                   <?php if(strtotime($date) + 86400 >= time()) :
                    $didOutputEventPost = true;
                     ?>
                   
                    
                   <article id="post-<?php echo $event->ID ?>" <?php post_class(); ?>> <!-- start of post -->
                        <span class="post-title-wrapper"><span class="post-title"><a href="<?php echo get_permalink($event->ID) ?>" rel="bookmark" title="<?php printf(__('Link to %s', 'responsive'), the_title_attribute('echo=0')); ?>"><?php echo get_the_title($event->ID); ?></a></span></span><br/>
                        
                        <span class="icn date"><?php echo substr($meta['start_date'], 0, 4); ?>-<?php echo substr($meta['start_date'], 4, 2); ?>-<?php echo substr($meta['start_date'], 6, 2); ?> 
                    <?php
                    if(isset($meta['end_date']) && $meta['end_date'] > $meta['start_date'] ) { ?>
                            -
                           <?php echo substr($meta['end_date'], 0, 4); ?>-<?php echo substr($meta['end_date'], 4, 2); ?>-<?php echo substr($meta['end_date'], 6, 2); } ?> 
                    
                    </span>
                    <?php
                    if(isset($meta['start_time'])) { ?>
                        <span class="icn time">
                           <?php echo $meta['start_time']; } ?> 
                    <?php
                            if(isset($meta['end_time'])) { ?>
                            - 
                           <?php echo $meta['end_time']; } ?>   
                        
                        </span>
                    <span class="icn place">
                        <?php echo $meta['location']; ?><br/>
                        <?php echo $meta['postal_code']; ?> <?php echo $meta['city']; ?>
                    </span>
                    
                    <?php if(isset($meta['price_non_members']) || isset($meta['price_members'])) { ?>
                    
                    <span class="icn price">
                        <?php if(isset($meta['price_members'])){ echo 'Members: '. $meta['price_members']; } ?><br/>
                        <?php if(isset($meta['price_non_members'])){ echo 'Non-members: '. $meta['price_non_members']; } ?>
                        
                    </span>
                    <?php } ?>
                    <div class="post-entry">    
                    <a href="<?php echo get_permalink($event->ID) ?>" rel="bookmark" title="<?php printf(__('Link to %s', 'responsive'), the_title_attribute('echo=0')); ?>"><?php if(isset($meta['image'])) { ?>
                       <img class="event-image" src="<?php echo $meta['image']; ?>" width="150"/></a>
					   <?php } ?>
                        
                    
                        <?php $descr = substr($meta['description'],0,300).'...';                     
                        echo $descr; ?><br/>
                        <a href="<?php echo get_permalink($event->ID) ?>" rel="bookmark" title="<?php printf(__('Link to %s', 'responsive'), the_title_attribute('echo=0')); ?>">Read more</a>
                    </div> <!-- end of .post-entry -->
                    </article> <!-- end of .post -->
                    
                   <?php endif; ?>  
                  
                <?php } ?>
                
                <?php if(!$didOutputEventPost) : ?>
                <p>There are unfortunately no upcoming events to display at this moment, but hold on – soon we will announce a new moment to get together. In the  meanwhile, make sure to sign up for the <a href="<?php echo esc_url(home_url('/')); ?>news/newsletter">newsletter</a> to ensure you don't miss out on the next one.</p> 
                <?php endif; ?>
                
				</div><!-- end of #upcomingevents -->

Can you show us what the query string looks like? Post back a print_r() on the $eventPosts.

Thank you for taking the time!
Was not sure where to put the print, so I removed the comment from where it was before. This is including all the content from the events so it’s messy… Sorry about that :-/ not sure if this is what you were looking for.

Another thing, if I remove the if statement to limit the results to only upcoming events they show up in a weird order – not DESC nor ASC.


Array ( [0] => WP_Post Object ( [ID] => 1000003022 [post_author] => 11 [post_date] => 2014-06-14 13:50:35 [post_date_gmt] => 2014-06-14 11:50:35 [post_content] => [post_title] => Back to Work BBQ, August in Region Dalarna [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => back-to-work-bbq-august-in-region-dalarna [to_ping] => [pinged] => [post_modified] => 2014-08-14 01:54:21 [post_modified_gmt] => 2014-08-13 23:54:21 [post_content_filtered] => [post_parent] => 0 [guid] => http://www.dutchchamber.se/?p=1000003022 [menu_order] => 0 [post_type] => post [post_mime_type] => [comment_count] => 0 [filter] => raw ) [1] => WP_Post Object ( [ID] => 1000002944 [post_author] => 14 [post_date] => 2014-04-09 21:03:34 [post_date_gmt] => 2014-04-09 19:03:34 [post_content] => [post_title] => Presentation and mingle at Media Evolution City [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => presentation-and-mingle-at-media-evolution-city [to_ping] => [pinged] => [post_modified] => 2014-06-25 17:32:51 [post_modified_gmt] => 2014-06-25 15:32:51 [post_content_filtered] => [post_parent] => 0 [guid] => http://www.dutchchamber.se/?p=1000002944 [menu_order] => 0 [post_type] => post [post_mime_type] => [comment_count] => 0 [filter] => raw ) [2] => WP_Post Object ( [ID] => 1000001914 [post_author] => 11 [post_date] => 2013-12-15 21:34:35 [post_date_gmt] => 2013-12-15 20:34:35 [post_content] => On March 21, the Dutch Chamber and many of its members and relations will celebrate the 400 year relationship between Sweden and The Netherlands. A relationship of diplomacy, culture, trade, architecture, art, innovation and friendship. Save the date for a special gala dinner, a tribute to 400 years of friendship. Location will be Stockholm. kleinsv-nl-400-years_logotype [post_title] => Annual dinner: a tribute to 400 Years of friendship [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => annual-dinner-a-tribute-to-400-years-of-friendship [to_ping] => [pinged] => [post_modified] => 2014-04-10 15:04:51 [post_modified_gmt] => 2014-04-10 13:04:51 [post_content_filtered] => [post_parent] => 0 [guid] => http://www.dutchchamber.se/?p=1000001914 [menu_order] => 0 [post_type] => post [post_mime_type] => [comment_count] => 0 [filter] => raw ) [3] => WP_Post Object ( [ID] => 1000002173 [post_author] => 17 [post_date] => 2014-02-10 22:41:19 [post_date_gmt] => 2014-02-10 21:41:19 [post_content] => Invitation Executive Luncheon Swedish Taste 28th Of Feb 11.30-13.30 The Dutch Chamber of Commerce Region West cordially invites you to an Executive Luncheon, an exclusive event for which we invite the executive level of our most loyal members. Our guest speaker is Ruben Brunsveld. He will give an interactive lecture on “How to get your message across”, including speech analysis of great speeches by Jan Eliasson, Peter van Uhm and Barack Obama. The location for this event is Restaurant Swedish taste, a restaurant with one of the best views of Gothenburg. Please let us know if you will be able to join us. Kind Regards, Celesta and Monique Swedish Taste St Eriksgatan 6, Göteborg Opposite of The Opera House of Gothenburg. 28th of February 2014 11.30-13.30 [post_title] => Executive Luncheon Region West [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => executive-luncheon-region-west [to_ping] => [pinged] => [post_modified] => 2014-03-13 21:34:51 [post_modified_gmt] => 2014-03-13 20:34:51 [post_content_filtered] => [post_parent] => 0 [guid] => http://www.dutchchamber.se/?p=1000002173 [menu_order] => 0 [post_type] => post [post_mime_type] => [comment_count] => 0 [filter] => raw ) [4] => WP_Post Object ( [ID] => 1000003032 [post_author] => 11 [post_date] => 2014-06-14 13:56:32 [post_date_gmt] => 2014-06-14 11:56:32 [post_content] => Save the date 12th of November 2014 16.00-19.00 Chalmerska Huset Gothenburg Region West International Networking in Gothenburg – focusing on how the city can be more attractive to foreign-owned companies- On the 12th of November, Business Region Gothenburg, together with the Dutch Chamber of Commerce, will arrange an international networking seminar with several foreign chambers of commerce and foreign-owned companies. We invite you all our members to this regional event, so that we can make this an amazing international event! More information will follow in the coming months! [post_title] => International Networking in Gothenburg – focusing on how the city can be more attractive to foreign-owned companies- [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => international-networking-in-gothenburg-focusing-on-how-the-city-can-be-more-attractive-to-foreign-owned-companies [to_ping] => [pinged] => [post_modified] => 2014-06-14 13:56:32 [post_modified_gmt] => 2014-06-14 11:56:32 [post_content_filtered] => [post_parent] => 0 [guid] => http://www.dutchchamber.se/?p=1000003032 [menu_order] => 0 [post_type] => post [post_mime_type] => [comment_count] => 0 [filter] => raw ) [5] => WP_Post Object ( [ID] => 1000003062 [post_author] => 17 [post_date] => 2014-06-25 13:55:22 [post_date_gmt] => 2014-06-25 11:55:22 [post_content] => In 2014 the Netherlands and Sweden celebrate 400 years of friendship between our two countries. Diplomatic relations were established in The Hague in 1614 as the first Swedish embassy in the world was inaugurated. During this celebration year various events are organized to commemorate this special occasion. The conference in Göteborg, 24th/25th September “lunch to lunch, will focus on the development of trade between our countries, and in a global perspective: “Sweden and the Netherlands-relations in a knowledge economy”. We will discuss the competences required and the industry needs with respect to education, research and innovation. We will conclude both days with a panel discussion. The conference will take place at Handelshögskolan Göteborg. This event is arranged by LifHof AB in cooperation with the Consulate General of the Netherlands. Details of the programme are being finalized and will be completed in August 2014. This event will be free of charge thanks to our sponsors listed below; [post_title] => Sweden and the Netherlands – relations in a knowledge economy- Trade conference “400 years” 24th/25th of September 2014 in Gothenburg [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => sweden-and-the-netherlands-relations-in-a-knowledge-economy-trade-conference-400-years-24th25th-of-september-2014-in-gothenburg [to_ping] => [pinged] => [post_modified] => 2014-06-25 14:15:43 [post_modified_gmt] => 2014-06-25 12:15:43 [post_content_filtered] => [post_parent] => 0 [guid] => http://www.dutchchamber.se/?p=1000003062 [menu_order] => 0 [post_type] => post [post_mime_type] => [comment_count] => 0 [filter] => raw ) [6] => WP_Post Object ( [ID] => 1000003087 [post_author] => 15 [post_date] => 2014-08-05 12:49:31 [post_date_gmt] => 2014-08-05 10:49:31 [post_content] => [post_title] => Back to Work Mingle Stockholm [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => back-to-work-mingle-stockholm-2 [to_ping] => [pinged] => [post_modified] => 2014-08-05 12:49:31 [post_modified_gmt] => 2014-08-05 10:49:31 [post_content_filtered] => [post_parent] => 0 [guid] => http://www.dutchchamber.se/?p=1000003087 [menu_order] => 0 [post_type] => post [post_mime_type] => [comment_count] => 0 [filter] => raw ) [7] => WP_Post Object ( [ID] => 1000002967 [post_author] => 11 [post_date] => 2014-04-13 18:50:54 [post_date_gmt] => 2014-04-13 16:50:54 [post_content] => All our members are welcome at our Annual Meeting [post_title] => Annual Meeting Dutch Chamber of Commerce [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => annual-meeting-dutch-chamber-of-commerce [to_ping] => [pinged] => [post_modified] => 2014-06-25 17:32:13 [post_modified_gmt] => 2014-06-25 15:32:13 [post_content_filtered] => [post_parent] => 0 [guid] => http://www.dutchchamber.se/?p=1000002967 [menu_order] => 0 [post_type] => post [post_mime_type] => [comment_count] => 0 [filter] => raw ) [8] => WP_Post Object ( [ID] => 1000002953 [post_author] => 17 [post_date] => 2014-04-10 11:40:27 [post_date_gmt] => 2014-04-10 09:40:27 [post_content] => [post_title] => Gothenburg’s International Gateway [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => gothenburgs-international-gateway [to_ping] => [pinged] => [post_modified] => 2014-05-26 22:29:53 [post_modified_gmt] => 2014-05-26 20:29:53 [post_content_filtered] => [post_parent] => 0 [guid] => http://www.dutchchamber.se/?p=1000002953 [menu_order] => 0 [post_type] => post [post_mime_type] => [comment_count] => 0 [filter] => raw ) [9] => WP_Post Object ( [ID] => 1000002220 [post_author] => 11 [post_date] => 2014-04-10 11:20:26 [post_date_gmt] => 2014-04-10 09:20:26 [post_content] => On Monday 14 April from 12:00 - 15:00 we will be hosting an executive lunch in the Board Room of Handelsbanken. We will be joined by: Pär Boman, Group CEO of Svenska Handelsbanken Håkan Sandberg, Vice CEO Jan Häggström, Chief Economist Mikael Sørensen, Head of Handelsbanken Netherlands After a lunch with magnificent views over Stockholm, they will share their thoughts and opinions on doing business in Sweden and the Netherlands, the 400 years of diplomatic relations and the current economic and political circumstances. [post_title] => Executive Lunch @ Handelsbanken [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => executive-lunch-handelsbanken [to_ping] => [pinged] => [post_modified] => 2014-04-16 13:10:25 [post_modified_gmt] => 2014-04-16 11:10:25 [post_content_filtered] => [post_parent] => 0 [guid] => http://www.dutchchamber.se/?p=1000002220 [menu_order] => 0 [post_type] => post [post_mime_type] => [comment_count] => 0 [filter] => raw ) [10] => WP_Post Object ( [ID] => 1000002357 [post_author] => 15 [post_date] => 2014-03-25 19:33:49 [post_date_gmt] => 2014-03-25 18:33:49 [post_content] =>
Randstad Award 2014

Med 4900 medverkande företag i 23 länder är Randstad Award världens största employer branding-undersökning. Syftet är att besvara en enda fråga: vilket företag är den mest attraktiva arbetsgivaren? Vi har precis avslutat sammanställningen av resultaten från de 8000 intervjuer som avgör vinnaren av Sveriges allra första Randstad Award.

Vill du veta vilket företag som utnämns till Sveriges mest attraktiva arbetsgivare ska du anmäla dig till prisutdelningen som tar plats på Nalen i Stockholm den 3:e april!

Program

Kvällens konferencier är Jenny Strömstedt, som tillsammans med inspirerande föreläsare, som employer branding-gurun Brett Minchington, och årets vinnare kommer bjuda på en rolig, lärorik och spännande kväll.

17.00 Dörrarna öppnas 17.30 Föreläsning 18.30 Prisutdelning 19.30 Buffé & mingel ca 22.00 Avslut

Anmäl dig på www.randstad.se/anmalan Inbjudan gäller för dig och en kollega. Varmt välkommen till Sveriges första Randstad Award!

www.randstad.se/award [post_title] => Randstad Award 2014 [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => randstad-award-2014 [to_ping] => [pinged] => [post_modified] => 2014-03-25 19:33:56 [post_modified_gmt] => 2014-03-25 18:33:56 [post_content_filtered] => [post_parent] => 0 [guid] => http://www.dutchchamber.se/?p=1000002357 [menu_order] => 0 [post_type] => post [post_mime_type] => [comment_count] => 0 [filter] => raw ) )

Ok so I know almost nothing about wordpress, but based on the array you posted, I don’t think you’re querying the correct table column. There isn’t any start_date field in the returned results. Maybe you meant to query for post_date? If you can post the actual query string that is being sent to the db to get the results, then maybe we can narrow this down.

Yes, you are right. There is no start_date, it looks like these values are all from wordpress posts. Above that I’m using Advanced Custom Fields plugin to add the start_date of the event + other values, and thats what I want it to be sorted by. I know nothing about php so this will be a challenge for you… so sorry about that. How would I get you the query you need to take this further?

I have no idea, you may want to post something on a wordpress specific forum, they will have a much better idea of what to do.

Thank you for your help! I think I will try to go for a event plugin instead since this is driving me insane…

Well, you passed ARGs to the WP code using this line:
‘orderby’ => ‘meta_value’,

So, you told it to sort DESC using this field. Perhaps you should alter the field you have told it to
order-by… Might help…

Thanks for your help! What should I alter it to in order to make them display correctly? Sorry, but I have no idea. I’ve been looking a bit at ACF, and I think that the guy who coded this used this site as reference: http://www.advancedcustomfields.com/resources/orde-posts-by-custom-fields/

Well, this is hard to explain to someone who doesn’t know about programming WordPress…

WP is a back-end type of program. It uses your decisions in the WP control panel to decide on how
to build the pages that are displayed. Therefore, a lot of the code is not really code, but arguments
passed to WP.

In your code sample you show the PHP code that creates arguments to send to the WP back-end.
This will control what is shown and how it is handled such as with sorting. So, first you would need
to know what fields are in your database. Most likely you can just look at the control panel for this
or you can actually look at the database itself in your hosting control panel.

Once you know what you want to sort by, you alter the “order-by” field to order your displayed values
by the correct field you want to use. In other words, right now, it sorts descending which is set inside
the ORDER arg and uses the ORDERBY field named “meta_value”, whatever that is. So, change that to
the field you want to sort by and it should work for you.

But, you really need to know what the “meta_value” field is. Since this is really a WordPress question
and not a standard PHP problem, you might want to ask it in their home site. they have a forum there
that might be able to help you better than we can…

Hope that helps…

Sponsor our Newsletter | Privacy Policy | Terms of Service