Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
What file are you placing this code into? The single template file? –  t31os Nov 17 '10 at 15:26

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
@Rast thanks for the respond I do have now separate field for month and year.. .thanks you so much... . –  idontknowhow Nov 19 '10 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... –  onetrickpony Dec 17 '10 at 11:36

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); 
share|improve this answer
    
Please don't encourage the use of query_posts. –  s_ha_dum Dec 17 '13 at 22:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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