如何通过自定义字段日期来订购wordpress文章?

时间:2020-12-05 22:45:21

I am making an event sidebar section that will only display the next 3 events. I have got the custom post type and custom fields all working but I can seem to figure out how to order the posts by the start date of the events, which is a custom field value. Is there a php function that can compare dates and organize them into a certain order. I think it would also have to hold the post-id with the newly arranged dates so that when I read through the values, i can display the appropriate post with that date.

我正在制作一个事件侧栏,只显示接下来的3个事件。我已经获得了自定义post类型和自定义字段,但我似乎可以找到如何在事件的开始日期之前对这些文章进行排序,这是一个自定义字段值。是否有一个php函数可以比较日期并按一定的顺序组织它们。我认为它还必须保留新安排的日期的post-id,以便当我阅读这些值时,我可以显示相应的日期的post。

Does anyone have a certain direction to steer me in?

有人能给我指明方向吗?

I think this is what I need to do:

我想这就是我需要做的:

  1. Read through the posts and grab the dates
  2. 通读文章,找出日期
  3. Sort the dates with the post-id associated with those dates
  4. 用与日期相关的post-id对日期进行排序
  5. Read through the sorted dates and re-display the first 3 posts by post-id
  6. 阅读整理后的日期,并通过后id重新显示前3个帖子。

I get lost on how to code that though... This is what I have so far. This code just displays them by their publish dates in wordpress.

我迷失在如何编码……这是我目前所拥有的。这段代码只是通过他们在wordpress中的发布日期来显示它们。

<?php query_posts('post_type=events');
            if (have_posts()) : while (have_posts()) : the_post(); ?>
                <?php $dateStart = get_post_meta($post->ID, 'date-start', true);?>
                <div class="date"><?php echo $dateStart; ?></div>

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

1 个解决方案

#1


3  

I believe I was able to answer my own question. Wordpress has a built in feature to compare custom field values between posts. To compare dates, which was my custom field value, they have to be in 'yyyymmdd' format to easily be able to compare them.

我相信我能回答我自己的问题。Wordpress有一个内置功能,可以比较文章之间的自定义字段值。要比较日期(这是我的自定义字段值),它们必须是“yyyyymmdd”格式,才能方便地比较它们。

I was really surprised that I didnt have to make multiple loops and store post-ids or anything. Anyways, I hope this helps someone else. :]

我真的很惊讶,我不用做多次循环,也不用存储post-id之类的东西。无论如何,我希望这能帮助别人。:]

 <?php 
        $args = array(
           'post_type' => 'events',
           'posts_per_page' => 3, //limited myself to 3 posts
           'meta_key' => 'date-start', //name of custom field
           'orderby' => 'meta_value_num', 
           'order' => 'ASC'
        );
        query_posts($args);
        if (have_posts()) : while (have_posts()) : the_post(); ?>
            //Insert code here...
            //Test to see if it is sorting the posts (below)
            <?php $dateStart = get_post_meta($post->ID, 'date-start', true); echo $dateStart;?>

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

#1


3  

I believe I was able to answer my own question. Wordpress has a built in feature to compare custom field values between posts. To compare dates, which was my custom field value, they have to be in 'yyyymmdd' format to easily be able to compare them.

我相信我能回答我自己的问题。Wordpress有一个内置功能,可以比较文章之间的自定义字段值。要比较日期(这是我的自定义字段值),它们必须是“yyyyymmdd”格式,才能方便地比较它们。

I was really surprised that I didnt have to make multiple loops and store post-ids or anything. Anyways, I hope this helps someone else. :]

我真的很惊讶,我不用做多次循环,也不用存储post-id之类的东西。无论如何,我希望这能帮助别人。:]

 <?php 
        $args = array(
           'post_type' => 'events',
           'posts_per_page' => 3, //limited myself to 3 posts
           'meta_key' => 'date-start', //name of custom field
           'orderby' => 'meta_value_num', 
           'order' => 'ASC'
        );
        query_posts($args);
        if (have_posts()) : while (have_posts()) : the_post(); ?>
            //Insert code here...
            //Test to see if it is sorting the posts (below)
            <?php $dateStart = get_post_meta($post->ID, 'date-start', true); echo $dateStart;?>

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