dsc71976 2014-09-01 10:54
浏览 20
已采纳

通过自定义元日期订购帖子

Below is my code which loads a custom post type (show), for some reason whatever I change within the $args nothing changes with the order.

I would like to display shows in date order.

The custom field test_date is in the format of 20140907

Anyone able to shed some light?

 <?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();  ?>
  • 写回答

1条回答 默认 最新

  • douwang4374 2014-09-01 12:00
    关注

    Its ok, I have managed to do it another way with SQL and that is working perfect!

    For reference:

    <?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; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行