Wordpress ACF插件 - 如何按日期排序

时间:2015-09-12 17:50:29

标签: wordpress date advanced-custom-fields

我正在使用高级自定义字段插件在自定义帖子中创建活动日期。我想在那个日期之前订购我的活动,但它对我不起作用:

<div class="column one">

                    <?php 

                        $args = array( 
                            'post_type'         => 'formation',
                            'posts_per_page'    => -1,
                            'post_status' => 'publish',
                            'meta_key'          => 'date_formation',
                            'orderby'           => 'meta_value_num',
                            'order'             => 'DESC'                   

                        ); 
                        $posts_query = new WP_Query( $args );

                        while ($posts_query->have_posts()) : $posts_query->the_post();

                        $presentation = get_field('date_formation');
                        $date_formation = mysql2date( 'j F Y ', $presentation);

                        $lieu = get_field('adresse_formation');
                    ?>

                            <div class="column one-third eventpost">
                                <div class="event-col">
                                 <?php  echo '<h4>'.$date_formation.''.$lieu.'</h4>'; ?> 
                                 <?php if( !is_single() ) {?><?php the_excerpt(); ?> <?php }else{ ?>
                                  <?php the_content(); ?>
                                  <?php } ?>
                                  <p><a href="<?php the_permalink(); ?>" rel="bookmark" title="En savoir plus">En savoir plus<i class="icon-right-open"></i></a></p>    
                                 </div>
                            </div>
                        <?php endwhile; wp_reset_query(); ?>

               </div>

1 个答案:

答案 0 :(得分:0)

尝试使用来自this question的Parham解决方案。您需要稍微修改它:

<div class="column one">
<?php
    function wpse_130954_orderby_fix($pieces){
    global $wpdb;
    $pieces['where']  .= " AND $wpdb->postmeta.meta_key = 'date_formation'";
    $pieces['orderby']  = "$wpdb->postmeta.meta_value DESC";
    return $pieces;
    }
    add_filter( 'posts_clauses', 'wpse_130954_orderby_fix', 20, 1 );
    $args = array(
        'post_type' => 'formation',
        'posts_per_page'=> -1,
        'post_status' => 'publish',
    ); 
    $posts_query = new WP_Query( $args );
    while ($posts_query->have_posts()) : $posts_query->the_post();
        $presentation = get_field('date_formation');
        $date_formation = mysql2date( 'j F Y ', $presentation);
        $lieu = get_field('adresse_formation');
?>
    <div class="column one-third eventpost">
        <div class="event-col">
            <?php echo '<h4>'.$date_formation.''.$lieu.'</h4>'; ?> 
            <?php if( !is_single() ) {?><?php the_excerpt(); ?><?php }else{ ?><?php the_content(); ?><?php } ?>
            <p><a href="<?php the_permalink(); ?>" rel="bookmark" title="En savoir plus">En savoir plus<i class="icon-right-open"></i></a></p>
        </div>
    </div>
    <?php endwhile; wp_reset_query(); ?>
    <?php remove_filter( 'posts_clauses', 'wpse_130954_orderby_fix', 20 ); ?>
</div>