Question

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/17/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 the 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/17/2010 compare to current date

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  | 
+-----------------------+-------------+

thanks guys

Was it helpful?

Solution

meta_key value should be name of custom field. I doubt you have fields named after each month, so it's likely should be eventdate.

meta_compare should have value that specifies a comparison operator, or just be omitted and it will default to = in that case.

From this you can't really query full event date on equals condition to month alone. I am not sure but as I see it you will have either to add separate field for month/year (without day) or do much more complex query with posts_where filter.

See Time Parameters, Custom Field Parameters in Codex for more documentation.

OTHER TIPS

Try This:

$archive_year  = get_the_time('Y'); 
$archive_month = get_the_time('m'); 
$archive_day   = get_the_time('d');
query_posts('year=' .$archive_year .'&monthnum=' .$archive_month .'&day=' .$archive_day); 
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top