Query post with current date month with custom field?
How to query Post with current date month with custom field:
Here is my code
<?php
global $wp_query;
$event_month = get_post_meta($wp_query->post->ID, 'eventdate', true); //format in db 11/15/2010
$event_month = date("n"); //A numeric representation of a month, without leading zeros (1 to 12)
$today= getdate(); ?>
<?php query_posts('meta_key='.$event_month .'&meta_compare&meta_value=' .$today["mon"]);?>
<?php while (have_posts()): the_post(); ?>
<div class="event-list-txt">
<h4><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark">
<?php the_title(); ?>
</a></h4>
<?php the_content(); //Display the content?>
</div>
<?php endwhile; ?>
<?php wp_reset_query();?>
but it doesn't display t开发者_开发问答he output.. .what I want to display is the event list for the month.. for example if it's November it will display the event list for November and when the month of December comes it will show the event list of December and so on.. . The event list date comes with custom field format like this 11/15/2010 compare to current date
i'm stack on it.. .thanks guys
here are the additional expected output current date is November and the event list should be like this:
+-----------------------+-------------+
| Event Name | Date |
+-----------------------+-------------+
| ABC Market opening | 11/18/2010 |
| ABC Market Seminar | 11/25/2010 |
| ABC Market Promo | 11/29/2010 |
+-----------------------+-------------+
I think your problem is here:
<?php query_posts('meta_key='.$event_month .'&meta_compare&meta_value=' .$today["mon"]);?>
...You're trying to find a custom field named "11" (Numerical representation of November) with value 11.
Check http://codex.wordpress.org/Function_Reference/query_posts#Time_Parameters for a code snippet that allows you to set the "WHERE" part of the query:
Return posts for posts for March 1 to March 15, 2009:
<?php
//Create a new filtering function that will add our where clause to the query
function filter_where($where = '') {
//posts for March 1 to March 15, 2009
$where .= " AND post_date >= '2009-03-01' AND post_date < '2009-03-16'";
return $where;
}
// Register the filtering function
add_filter('posts_where', 'filter_where');
// Perform the query, the filter will be applied automatically
query_posts($query_string);
?>
Hope this helps at all...
精彩评论