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.

I have custom post type. In this custom post type I have custom field with date in format: 23/09/2012

How to show only posts, which currently date is before date from these custom field?

Sory for my english.

Tom

share|improve this question
add comment

1 Answer

You can do this using query_posts() or a new WP_Query object. Here's an example:

query_posts( array(
    'post_type' => 'my_post_type',
    'meta_query' => array(
        array(
           'key' => 'my_date_field_key',
           'value' => date( 'd/m/Y' ),
           'compare' => '<',
           'type' => 'DATE'
        )
    )
) );

if ( have_posts() ) : while( have_posts() ) : the_post();

    // your loop code here
    the_title('<h2>','</h2>');

endwhile; endif;

wp_reset_query();

NOTE:

If you want to order by date this is a little trickier unless you store the date as a unix timestamp. Then you can orderby meta_value_num as it's just a number.

share|improve this answer
    
Hi, thanks for your answer. unfortunately it's not working for me. My code: <code>asdas</code> –  Tomek Mar 15 '12 at 12:03
    
Hi, thanks for your answer. unfortunately it's not working for me. My code: query_posts( array( 'post_type' => 'wydarzenia', 'meta_query' => array( array( 'key' => 'data_zakonczenia_wydarzenia', 'value' => date( 'd/m/Y' ), 'compare' => '<', 'type' => 'DATE' ) ) ) ); if ( have_posts() ) : while( have_posts() ) : the_post(); // my loop code here endwhile; endif; wp_reset_query(); and not working. I tried with another date format, but still not working? –  Tomek Mar 15 '12 at 12:17
    
Where it says '// your loop code here' you have to pu in something for it to output eg. the_title() –  sanchothefat Mar 15 '12 at 13:03
    
I would recommend storing dates as unix timestamps too, let me know if you make that change and I'll update the example. –  sanchothefat Mar 15 '12 at 13:06
add comment

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.