Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Create Future Archives for custom Event setup WordPress

  • SOLVED

We have a custom events category. It uses a query of future posts. We display 20 on the page. We want to have links to the future months so people can view events 1-11 months away. How ever we do not know how to build this query. We are stuck with the fact once we get to December.. our queries will still be working within 2010 and not roll over to 2011 automatically.

Answers (3)

2010-07-22

wjm answers:

Hi Overhaul,
Questions:

1. what is the date of the event? the date of the published post? or is it a custom field?
2. I would like to see your current code to see how you are doing it and build it on top of it.

but the answer may be similar to the asnwer i gave in this question
http://wpquestions.com/question/show/id/637

Looking forward to hearing from you
- wjm


Overhaul Media comments:

Hey wjm,

Date is the date of the post. They dont use posts for anything else on this site so it made sense to not use a custom field. Also makes it much easier to style the date how we have,

Here is what we're using to pull in the first 20

<?php query_posts('showposts=20&category_name=e&post_status=future&order=ASC'); ?>


wjm comments:

Look, this is how you would do it,

<ul>
<?php
function filter_where($where = '') {
$where .= " AND post_date >= DATE( NOW() ) AND post_date <= DATE_ADD(CURDATE(),INTERVAL 1 YEAR)";
return $where;
}
add_filter('posts_where', 'filter_where');
$my_query = new WP_Query(
array(
'post_type'=>'post',
'posts_per_page' => 20,
//'orderby' => 'date',
'order' => 'ASC',
'category_name' => 'e',
'post_status' => 'publish,future',
)
);
while ( $my_query->have_posts() ) :
$my_query->the_post();
$do_not_duplicate = $post->ID;
?>
<li><a href="<?php the_permalink() ?>"><?php the_date( 'n/d'); ?> <?php the_title(); ?></a></li>
<?php
endwhile;
remove_filter('posts_where', 'filter_where');
?>
</ul>


I will try to adapt this code to work just with get_posts() so you dont have modify much code.
but the above code works.

@Oleg: not yet! ;^D


Overhaul Media comments:

This is looking good, but I do not see how we can query by month in the code. I need a page that only displays June 2011, October 2010... etc.


wjm comments:

I see,

This is the simplified code, to work with get_posts().

$args = array(
'post_type'=>'post',
'posts_per_page' => 20,
//'orderby' => 'date',
'order' => 'ASC',
'category_name' => 'e',
'post_status' => 'publish,future',
);
$events = query_posts( array_merge($args, array( 'monthnum' => '7', 'year' => '2010',


just set
array( 'monthnum' => '7', 'year' => '2010', )

to whatever your year and month is.

you dont need the filter if you are doing that.

Let me know if that works
-wjm


wjm comments:

Sorry, you do need the filter.

I am sorry for the confusion
just use this code (the above was cut when i pasted it)


<?php
function filter_where($where = '') {
$where .= " AND post_date >= DATE( NOW() )";
return $where;
}
add_filter('posts_where', 'filter_where');

$args = array(
'post_type'=>'post',
'posts_per_page' => 20,
//'orderby' => 'date',
'order' => 'ASC',
'category_name' => 'e',
'post_status' => 'publish,future',
);
$events = query_posts( array_merge($args, array( 'monthnum' => '6', 'year' => '2011', )) );

//print_r($events);
remove_filter('posts_where', 'filter_where');
?>


Overhaul Media comments:

We can not hard code the years, because then every month we'll need to edit the files...

If I make all my templates for each month and link to them.. What will happen in 3 months.. I will have 3 wrong queries to change.


Overhaul Media comments:

I am not much of a PHP guy but we need to
1. get current month
2. add 1 month if we're linking to August, add 11 months if we're looking for June
3. display the results.

That way the years will never have to be updated as time goes by.


wjm comments:

I dont think you should be hardcoding the dates, that should be passes as a query var,
because that way you will have to have 12 templates.

here is the code for what you need,
having the year calculated automatically based on the month you set,

<?php
function filter_where($where = '') {
$where .= " AND post_date >= DATE( NOW() )";
return $where;
}
add_filter('posts_where', 'filter_where');

$date = getdate();
$month = 7;
$year = ( $month < $date['mon'] ) ? $date['year'] +1 : $date['year'];
$args = array(
'post_type'=>'post',
'posts_per_page' => 20,
//'orderby' => 'date',
'order' => 'ASC',
'category_name' => 'e',
'post_status' => 'publish,future',
'monthnum' => $month,
'year' => $year,
);
$events = query_posts( $args );

//print_r($events);
remove_filter('posts_where', 'filter_where');
?>


just set $month = 7; to whatever you need.

i dont quiet understand what you said in points 1, 2 and 3, but let me know if this is what you need or not.



Overhaul Media comments:

Sorry, here is our current code.. where would yours go?

<ul>

<?php query_posts('showposts=20&category_name=e&post_status=future&order=ASC'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<li style="list-style:none;">

<div class="date" style="float:left; padding-right:20px;">
<div class="date-inside">
<div class="date-day"><?php the_time('D'); ?></div>
<div class="date-month"><?php the_time('M'); ?></div>
<div class="date-day2"><?php the_time('d'); ?></div>
</div>
</div>

<div style="float:left; padding-right:20px;"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a></div>
<div style="float:left; padding-right:20px;"> <img src="<?php bloginfo(template_url); ?>/images/tweet.gif" /><a href="http://twitter.com/home?status=<?php the_title(); ?> | <?php the_permalink(); ?>" style="font-size:10px;color:#757575; text-decoration:none;"> Tweet this</a> &nbsp; <span style="font-size:10px; color:#6f6f6f;">|</span> &nbsp; <img src="<?php bloginfo(template_url); ?>/images/facebook.gif" /> <a href="http://www.facebook.com/sharer.php?u=<?php the_permalink(); ?>&t=<?php the_title(); ?>" style="font-size:10px; color:#757575; text-decoration:none;">Share This</a> </div>
<div style="float:left; padding-right:20px;"> <h3 style="font-size:18px;"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> </div>
<div clear="all"></div>
</li>



<?php endwhile; else: ?>
<li>Sorry, no upcoming events!</li>
<?php endif; ?>
</ul>


wjm comments:

just replace your get_posts() call

<?php query_posts('showposts=20&category_name=e&post_status=future&order=ASC'); ?>

with my code

2010-07-22

Rashad Aliyev answers:

Hello,
You can use Custom Fields for this.


Rashad Aliyev comments:

Use this on your wp theme. Make custom fields for example: futuredate, then use it on your theme for DATE.


<?php $date="futuredate"; echo get_post_meta($post->ID, $date, true); ?>

That's all!

2010-07-22

Oleg Butuzov answers:

solved by <strong>wjm</strong>