0

I have posts which I want to order by custom fields ... I'm using Advanced Custom Fields plugin for my custom fields. here is my code... pleas help me to modify it so that the posts would be orderd by start_day custom field...

 $today = date('Y-m-d');
    $cat = get_term_by('name', 'events', 'category');
    $args = array('cat' => $cat->term_id, 'posts_per_page' => -1, 'meta_query' => array(
        array(
            'key' => 'end_date',
            'value' => $today,
            'type' => 'DATE',
            'compare' => '>=',
            'meta_key' => '_date',
            "orderby" => "start_date",
            "order" => "ASC"
        )));

    $posts = get_posts($args);

    foreach($posts as $post) {
        setup_postdata($post);

        if((date("j", strtotime(get_field('start_date')))+1 || date("j", strtotime(get_field('start_date')))) !=date("j", strtotime(get_field('end_date')))){
        $data_info = date("F", strtotime(get_field('end_date')))." ".date("d", strtotime(get_field('start_date')))."-".
            date("d", strtotime(get_field('end_date')));
        }else{
            $data_info = date("F", strtotime(get_field('end_date')))." ".date("d", strtotime(get_field('start_date')));
        }
        ?><div class="fEventFront">
        <?php
        if (has_post_thumbnail($post->ID)):
        $thumbnail = wp_get_attachment_image_src ( get_post_thumbnail_id($post->ID), array(55,55));
        $full = wp_get_attachment_image_src ( get_post_thumbnail_id($post->ID), 'Large');
        ?>

        <div class="fEventFrontThumb">
            <img width="55" src="<?php echo $thumbnail[0];?>" alt=""/>
        </div>
        <?php endif; ?>
        <div class="fEventFrontText">
            <div class="fEventFrontTitle"><span class="date_info"><?php echo $data_info?> </span><?php the_title() ?></div>
            <div class="fEventFrontDescription"><?php the_field('short_description') ?></div>
        </div>
    </div><?php
    }
encoder
  • 58
  • 2
  • 9
  • Your code looks ok - what's the issue? – Jure C. Aug 18 '12 at 11:27
  • Yes my code is working... but I need to display posts order by custom field... the field name is start_day... but whit this code i'm not able to display it ordered by the custom field which is a date of the event start day... but I'm not able to do so... plz help me... – encoder Aug 18 '12 at 11:31

1 Answers1

1

You've already got meta_key as one of the arguments so now you should just need to change the orderby argument to;

'orderby' => 'meta_value' 

Wordpress WP_Query orderby options

However, this is going to be complicated as you have a date as your custom field (at least I think you do, you've referred to it as both start_day and start_date in your question).

Depending on how you have done that you may have to first convert that date to a numerical format and sort it using;

'orderby' => 'meta_value_num' 
McNab
  • 6,767
  • 4
  • 35
  • 55