0

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

1
  • What file are you placing this code into? The single template file?
    – t31os
    Commented Nov 17, 2010 at 15:26

2 Answers 2

1

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.

2
  • @Rast thanks for the respond I do have now separate field for month and year.. .thanks you so much... . Commented Nov 19, 2010 at 2:31
  • 1
    I would do this by creating a custom meta box with a datepicker field/form which generates a custom field with the date in UNIX timestamp format. Then in the front end you can easily compare dates using this timestamp... Commented Dec 17, 2010 at 11:36
0

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); 
1
  • Please don't encourage the use of query_posts.
    – s_ha_dum
    Commented Dec 17, 2013 at 22:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.