• Resolved zaydB

    (@zaydb)


    Hey guys,

    Have a perplexing problem. Im trying to get some events going with custom start and end dates as custom fields. They both use the format ‘YYYYMMDD’. The problem I have is that the code seems to work fine on one tab, but starts ordering the posts randomly on the next few. I copied and pasted the code exactly.

    <!-- Tabs  for current month -->
    <?php
    $dateM = date('m');
    $dateY = date('Y');// Current Year
    $args = array('post_type' => 'events', 'numberposts' => '5', 'order_by' => 'event_start','order' => 'asc',
    			   'meta_query' => array( 'relation' => 'AND',
    			   				   array('key'=>'Event Month Start', 'value' => $dateM, 'compare' => '=', 'type' => 'NUMERIC'),
    			   				   array ('key'=>'Event Year Start', 'value' => $dateY, 'compare' => '=', 'type' => 'NUMERIC')
    			   				   	    )
    			  );
    $recent_posts = get_posts($args);
    foreach($recent_posts as $recent) : setup_postdata($recent);
    $image = wp_get_attachment_image(get_post_thumbnail_id($recent->ID),'events-loop');
    $event_start_S = get_post_meta($recent->ID,'event_start',true);
    $event_start = date('j F Y', strtotime($event_start_S));
    $event_end_S = get_post_meta($recent->ID,'event_end',true);
    $event_end = date('j F Y', strtotime($event_end_S));
    ?>
    
    				<article>
                      <div class="pic"><a href="<?php echo get_the_permalink($recent);?>" class="w_hover img-link img-wrap"><?php echo $image; ?><span class="link-gallery"></span> </a> </div>
                      <h3><a href="<?php echo get_the_permalink($recent); ?>"><?php echo get_the_title($recent); ?></a></h3>
                      <ul class="icons">
                        <li><a href="<?php echo get_the_permalink($recent); ?>" class="post_date"><?php echo $event_start;?> - <?php echo $event_end;?></a></li>
                        <li><a href="" class="comments_count"></a></li>
                      </ul>
                       <div class="text"><?php echo get_excerpt(120,$recent); ?></div>
                    </article>
    
    <?php endforeach;
    wp_reset_postdata(); ?>

    That is the code for the current month and this:

    <!-- Tabs  for current month -->
    <?php
    $dateM = date('m')+5;
    $dateY = date('Y')+1;// Set the Year 1 year ahead, i have a switch statement here to check
    $args = array('post_type' => 'events', 'numberposts' => '5', 'order_by' => 'event_start','order' => 'asc',
    			   'meta_query' => array( 'relation' => 'AND',
    			   				   array('key'=>'Event Month Start', 'value' => $dateM, 'compare' => '=', 'type' => 'NUMERIC'),
    			   				   array ('key'=>'Event Year Start', 'value' => $dateY, 'compare' => '=', 'type' => 'NUMERIC')
    			   				   	    )
    			  );
    $recent_posts = get_posts($args);
    foreach($recent_posts as $recent) : setup_postdata($recent);
    $image = wp_get_attachment_image(get_post_thumbnail_id($recent->ID),'events-loop');
    $event_start_S = get_post_meta($recent->ID,'event_start',true);
    $event_start = date('j F Y', strtotime($event_start_S));
    $event_end_S = get_post_meta($recent->ID,'event_end',true);
    $event_end = date('j F Y', strtotime($event_end_S));
    ?>
    
    				<article>
                      <div class="pic"><a href="<?php echo get_the_permalink($recent);?>" class="w_hover img-link img-wrap"><?php echo $image; ?><span class="link-gallery"></span> </a> </div>
                      <h3><a href="<?php echo get_the_permalink($recent); ?>"><?php echo get_the_title($recent); ?></a></h3>
                      <ul class="icons">
                        <li><a href="<?php echo get_the_permalink($recent); ?>" class="post_date"><?php echo $event_start;?> - <?php echo $event_end;?></a></li>
                        <li><a href="" class="comments_count"></a></li>
                      </ul>
                       <div class="text"><?php echo get_excerpt(120,$recent); ?></div>
                    </article>
    
    <?php endforeach;
    wp_reset_postdata(); ?>

    The first query works and I can put the query on different tabs and it’ll work fine with the dates in order. The other tabs won’t even show the posts or would randomly order them and not order them. Please help.
    Regards
    Zayd

Viewing 13 replies - 1 through 13 (of 13 total)
  • Susan Langenes

    (@susanlangenes)

    Are the Event Month Start and Event Year Start custom fields?

    Thread Starter zaydB

    (@zaydb)

    Hi susan, yes they are. My code initially pulled this info from the event_start and event_end fields and it should have worked in theory but didn’t.

    Would this affect how the posts would be ordered? Why would it work on the first query and not the second?

    Susan Langenes

    (@susanlangenes)

    Funny, I deleted the content of my first comment b/c I couldn’t figure out how to delete the comment.. I read your original post and realize that you’d already stated they’re custom fields.

    Let me ask more dumb questions. So the current month query (first one above) works, but it breaks when you set the year to one year ahead and the month to 5 months ahead?

    Thread Starter zaydB

    (@zaydb)

    Yes, I actually have a 10 month cycle and they all behave this way

    Susan Langenes

    (@susanlangenes)

    I think some additional context would help. What are you trying to display? What do you mean by “tabs”?

    Thread Starter zaydB

    (@zaydb)

    absolutely, so my page has tabs for each month in a 10 month cycle. Each tab has its own template page running the query above with the month going from 0-10 respectively. To get a better idea you can go here I disabled permalink for testing

    Susan Langenes

    (@susanlangenes)

    Ah now that is indeed clearer. Thanks.

    So I see that for April 2016, it’s just totally broken.

    Try this in your $args?

    $args = array(
        'post_type'   => 'events',
        'posts_per_page' => '5',
        'meta_key'	=> 'event_start',
        'orderby'=> 'meta_value_num',
        'order' => 'ASC'
    );
    Susan Langenes

    (@susanlangenes)

    hang on, you’ll need more than that… jussec

    Thread Starter zaydB

    (@zaydb)

    Hi Susan,

    so far so good!!! It seems to be working with the amended code, in fact all I changed was the orderby value and boom. The dates are in order!! Strange thing is I have code for something similar for some other post types and that works fine. The magic of PHP and CMS systems! Thanks again Susan, you don’t have to put up the rest of the code if you don’t want to. I’ll do a bit more testing then close this topic.

    I owe you one. Thanks again

    Susan Langenes

    (@susanlangenes)

    Oh fantastic! Glad to hear that! ??

    Thread Starter zaydB

    (@zaydb)

    Yup, now before I close it Im just gonna test the years and see what happens

    Susan Langenes

    (@susanlangenes)

    I think the meta_query wasn’t necessary because you’re already specifying numbers in the variables before the args, and the ‘orderby’ parameter wasn’t making sense of ‘event_start’.

    Thread Starter zaydB

    (@zaydb)

    Excellent need to do more testing but so far so good!!

    Mark as Closed

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘order_by Custom Date’ is closed to new replies.