自定义字段的wordpress订单帖子

时间:2012-08-18 11:24:43

标签: wordpress wordpress-theming custom-fields

我有自定义字段要订购的帖子...我在自定义字段中使用高级自定义字段插件。这是我的代码...请求帮我修改它以便帖子将由start_day自定义字段排序...

 $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
    }

1 个答案:

答案 0 :(得分:1)

您已经将meta_key作为其中一个参数,所以现在您只需要将orderby参数更改为;

'orderby' => 'meta_value' 

Wordpress WP_Query orderby options

然而,由于您的自定义字段有一个日期(至少我认为您这样做了,您在问题中将其称为start_day和start_date),这将会很复杂。

根据您的方式,您可能必须先将该日期转换为数字格式并使用它进行排序;

'orderby' => 'meta_value_num'