【问题标题】:Wordpress ACF plugin - how to order by date fieldWordpress ACF 插件 - 如何按日期字段排序
【发布时间】:2015-09-14 17:22:56
【问题描述】:

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

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

【问题讨论】:

    标签: wordpress date advanced-custom-fields


    【解决方案1】:

    尝试使用来自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>
    

    【讨论】:

    • 我在我的 template-formation.php 中添加了代码,但不起作用
    • 'date_formation' 使用什么字段类型?它返回什么值?
    • 像这样:30.09.2015
    猜你喜欢
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 2020-09-20
    • 2018-09-11
    • 2018-04-25
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多