【问题标题】:Ordering posts via custom meta date通过自定义元日期订购帖子
【发布时间】:2014-09-01 10:54:02
【问题描述】:

下面是我加载自定义帖子类型(显示)的代码,出于某种原因,无论我在$args 中更改什么,订单都没有变化。

我想按日期顺序显示节目。

自定义字段test_date的格式为20140907

有没有人能解释一下?

 <?php 
    $args = array(
        'post_type' => 'show',
        'meta_key'  => 'test_Date',
        'orderby'   => 'meta_value_num',
        'order'     => 'DESC',
        'posts_per_page' => '-1'
    );
    query_posts($args);
    if (have_posts()) : while (have_posts()) : the_post(); ?>
        <?php
        $limit = 140;
        $display_from_date = get_post_meta( get_the_ID(), 'display_from_date', true );
        $display_to_date = get_post_meta( get_the_ID(), 'display_to_date', true );
        $book_online_link = get_post_meta( get_the_ID(), 'book_online_link', true );
        $show_description = get_post_meta( get_the_ID(), 'show_description', true );
        $date_formatted = get_post_meta( get_the_ID(), 'date_formatted', true );
        $show_content = get_the_content();
        $read_more = get_the_permalink();
        ?>

<div class="showbox">
  <div class="showthumb">
    <?php the_post_thumbnail('medium'); ?>
  </div>
  <div class="showdetails">
    <div class="showtitle"><?php the_title();?></div>
    <div class="showdate">
      <strong><?php echo $date_formatted; ?></strong>
    </div>
    <div class="clear"></div>
    <div class="showdescription">
      <br />
      <?php 
      if (empty($show_description)){
        echo '<em>No description available</em>';
      } else {
        if (strlen($show_description) > $limit)
        $show_description = substr($show_description, 0, strrpos(substr($show_description, 0, $limit), ' ')) . '... <a href="'. $read_more .'" style="font-size:10px; font-style: italic;">Read More</small>';
        echo $show_description;
      }
      ?>
    </div>
    <div class="clear"></div>

    <div class="showbuttons">
      <a href="<?php the_permalink(); ?>" class="moreinformation">More Information&nbsp;<IMG class='small_arrow_1_right' src='<?php bloginfo('template_url');?>/images/small_arrow_1_right.gif' alt='Next'>&nbsp;</a>
    </div>
    <div class="clear"></div>
  </div>
  <div class="clear"></div>
</div>

<?php endwhile; endif; wp_reset_query();  ?>

【问题讨论】:

    标签: php wordpress date custom-post-type meta


    【解决方案1】:

    没关系,我已经设法使用 SQL 以另一种方式做到这一点,而且效果很好!

    供参考:

    <?php
    
     $querydetails = "
       SELECT wposts.*
       FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
       WHERE wposts.ID = wpostmeta.post_id
       AND wpostmeta.meta_key = 'test_date'
       AND wposts.post_status = 'publish'
       AND wposts.post_type = 'show'
       ORDER BY wpostmeta.meta_value ASC
     ";
    
     $pageposts = $wpdb->get_results($querydetails, OBJECT)
    
     ?>
    
    <?php if ($pageposts):
     foreach ($pageposts as $post):
           setup_postdata($post);
    
      $limit = 140;
      $display_from_date = get_post_meta( get_the_ID(), 'display_from_date', true );
      $display_to_date = get_post_meta( get_the_ID(), 'display_to_date', true );
      $book_online_link = get_post_meta( get_the_ID(), 'book_online_link', true );
      $show_description = get_post_meta( get_the_ID(), 'show_description', true );
      $date_formatted = get_post_meta( get_the_ID(), 'date_formatted', true );
      $show_content = get_the_content();
      $read_more = get_the_permalink();
      ?>
    
    <div class="showbox">
      <div class="showthumb">
        <?php the_post_thumbnail('medium'); ?>
      </div>
      <div class="showdetails">
        <div class="showtitle"><?php the_title();?></div>
        <div class="showdate">
          <strong><?php echo $date_formatted; ?></strong>
        </div>
        <div class="clear"></div>
        <div class="showdescription">
          <br />
          <?php 
          if (empty($show_description)){
            echo '<em>No description available</em>';
          } else {
            if (strlen($show_description) > $limit)
            $show_description = substr($show_description, 0, strrpos(substr($show_description, 0, $limit), ' ')) . '... <a href="'. $read_more .'" style="font-size:10px; font-style: italic;">Read More</small>';
            echo $show_description;
          }
          ?>
        </div>
        <div class="clear"></div>
    
        <div class="showbuttons">
          <a href="<?php the_permalink(); ?>" class="moreinformation">More Information&nbsp;<IMG class='small_arrow_1_right' src='<?php bloginfo('template_url');?>/images/small_arrow_1_right.gif' alt='Next'>&nbsp;</a>
        </div>
        <div class="clear"></div>
      </div>
      <div class="clear"></div>
    </div>
    
     <?php endforeach;
    endif; ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-03
      • 2020-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多