0

我有想要按自定义字段排序的帖子...我正在为我的自定义字段使用高级自定义字段插件。这是我的代码...请帮我修改它,以便帖子将按 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
    }
4

1 回答 1

1

您已经获得meta_key了参数之一,所以现在您只需要将 orderby 参数更改为;

'orderby' => 'meta_value' 

Wordpress WP_Query orderby 选项

但是,这会很复杂,因为您有一个日期作为您的自定义字段(至少我认为您有,您在问题中将它称为 start_day 和 start_date)。

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

'orderby' => 'meta_value_num' 
于 2012-08-18T13:16:14.990 回答