按自定义字段值排序的表中的循环发布

时间:2015-07-07 作者:Kevin Mamaqi

我正在尝试为自定义post类型的事件生成一个表,其中有几个自定义字段,其中之一是日期。我想在表中显示帖子,并按日期值排序。这是我的代码:

Date format: 2015年7月7日(日/月/年)

目前,循环不起作用,我不知道如何按日期订购。

<div class="container">
  <div class="row">

        <table>
          <tr>
            <th>Artista</th>
            <th>Fecha</th>
            <th>Ciudad</th>
            <th>Información</th>
          </tr>
        <?php $i = 0;?>
        <?php while ( have_posts() ) : the_post(); ?>
            <?php if($i == 0){ echo "<tr>"; }?>
                <td><?php the_title(); ?></td>
                <td><?php the_field(\'fecha_del_evento\'); ?></td>
                <td><?php the_field(\'lugar_del_evento\'); ?></td>
                <td><?php the_content(); ?></td>
            <?php $i++;?>
            <?php if($i == 4){ echo "</tr>"; $i=0;}?>
        <?php endwhile; ?>
            <?php if($i < 4) echo \'</tr>\'; ?>
        </table>

        <?php else:  ?>
          <div class="page-header"><h1>Uh Oh!</h1></div>
          <p>Sorry, for some reason the contents of this page isn\'t displaying.</p>
        <?php endif; ?>


  </div><!-- END OF ROW -->
</div><!-- END OF CONTAINER -->

2 个回复
最合适的回答,由SO网友:Kevin Mamaqi 整理而成

我找到了一个解决方案,它可以显示按日期值排序的帖子,还可以删除过去的帖子。

<div class="container">
    <div class="row">  

     <table>
      <tr>
        <th>Artista</th>
        <th>Fecha</th>
        <th>Ciudad</th>
        <th>Información</th>
      </tr>

      <?php
      $today = current_time(\'Ymd\');

      $args = array(
        \'post_type\' => \'evento\',
        \'post_status\' => \'publish\',
        \'posts_per_page\' => \'-1\',
        \'meta_query\' => array(
          array(
            \'key\' => \'fecha_del_evento\',
            \'compare\' => \'>=\',
            \'value\' => $today,
            )
          ),
        \'meta_key\' => \'fecha_del_evento\',
        \'orderby\' => \'meta_value\',
        \'order\' => \'ASC\',
        );

      $query = new WP_Query($args);
      if ($query->have_posts()) :
        while ($query->have_posts()) : $query->the_post(); 
      ?>
      <?php $i = 0;?>
      <?php if($i == 0){ echo "<tr>"; }?>
      <td><?php the_title(); ?></td>
      <?php $i++;?>
      <td><?php the_field(\'fecha_del_evento\'); ?></td>
      <?php $i++;?>
      <td><?php the_field(\'lugar_del_evento\'); ?></td>
      <?php $i++;?>
      <td><?php echo get_the_content(); ?></td>
      <?php $i++;?>
      <?php if($i == 4){ echo "</tr>"; $i=0;}?>
    <?php endwhile; ?>
    <?php if($i < 4) echo \'</tr>\'; ?>
  </table>

  <?php wp_reset_postdata(); ?>

<?php endif; ?>
</div><!-- END OF ROW -->
</div><!-- END OF CONTAINER -->
请注意,此代码(当然在适当的查询中)足以按日期排序帖子:

\'meta_key\' => \'fecha_del_evento\',
            \'orderby\' => \'meta_value\',
            \'order\' => \'ASC\',
            );

SO网友:Michael Heimes

我有一个与你想要的相似的设置。此代码块位于模板文件中:

    <?php 

// get posts
$posts = get_posts(array(
    \'meta_key\'  => \'date\',
    \'orderby\'   => \'meta_value_num\',
    \'order\'     => \'ASC\'
));


// loop
if( $posts ) {

    foreach( $posts as $post ) {

        setup_postdata( $post );

        // do something

    }

    wp_reset_postdata();

}

?>                                  



<?php if ( have_posts() ) : ?>
    <table>
        <tbody>
            <tr class="schedule-header">
                <th scope="col" class="header-cell schedule-cell">Date</th>
                <th scope="col" class="header-cell schedule-cell">Time</th>
                <th scope="col" class="header-cell schedule-cell">Venue</th>
                <th scope="col" class="header-cell schedule-cell">Address</th>
                <th scope="col" class="header-cell schedule-cell">Tix</th>                              
            </tr>
        </tbody>    


<?php endif; ?>


<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    <?php get_template_part( \'template-parts/content-front-page\'); ?>
    <?php endwhile; else: ?>

        <h5>Sorry! Either I have been too lazy to put in my show dates or no one is booking me because they\'re mad at me. Are you mad at me?</h5>

<?php endif; ?>

    </table 
这段代码放在函数中。php:

function my_pre_get_posts( $query ) {

// do not modify queries in the admin
if( is_admin() ) {

    return $query;

}


// only modify queries for \'event\' post type
if( isset($query->query_vars[\'post\']) && $query->query_vars[\'post_type\'] == \'post\' ) {

    $query->set(\'orderby\', \'meta_value\');   
    $query->set(\'meta_key\', \'start_date\');   
    $query->set(\'order\', \'DESC\'); 

}


// return
return $query;

}

结束

相关推荐

Table is not created in WAMP

我不知道错在哪里。我的代码没有在WAMP中创建表:function subscribe_table() { global $wpdb; $table_name = $wpdb->prefix.\'_databasesubs\'; $charset_collate = $wpdb->get_charset_collate(); if ( $wpdb->get_var( \'SHOW TABLES LIKE