admin管理员组

文章数量:1296505

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

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. Sort the dates with the post-id associated with those dates
  3. Read through the sorted dates and re-display the first 3 posts by post-id

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.

<?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();  ?>

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

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. Sort the dates with the post-id associated with those dates
  3. Read through the sorted dates and re-display the first 3 posts by post-id

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.

<?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();  ?>
Share asked Jun 3, 2013 at 2:14 chasethesunnnchasethesunnn 2,2345 gold badges27 silver badges43 bronze badges 1
  • I think i would turn the $dateStart into an array and store the dates that way? I should probably grab the post-ids with those dates too, i think. – chasethesunnn Commented Jun 3, 2013 at 2:16
Add a ment  | 

1 Answer 1

Reset to default 8

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

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. :]

 <?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();  ?>

本文标签: phpHow to order wordpress posts by a custom field dateStack Overflow