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:
- Read through the posts and grab the dates
- Sort the dates with the post-id associated with those dates
- 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:
- Read through the posts and grab the dates
- Sort the dates with the post-id associated with those dates
- 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
1 Answer
Reset to default 8I 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
版权声明:本文标题:php - How to order wordpress posts by a custom field date? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741602064a2387760.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论