I found a handy plugin at http://www.redletterdesign.net/wp/cpt-archive/index.php for generating a calendar archive using a custom post type. My problem is I need it to organize these posts not by the post's publish date but by a custom date field (using Unix time). The custom post type is a custom event calendar which uses user-input (future) dates. Any idea as to how I would go about this?

1 Answer 1


Yep. You can add a meta_query parameter to methods that query posts, such as WP_Query. This one below gets a custom post type between two dates (which are custom fields created by 'advanced custom fields' plugin) and sorts on one of these dates:

$args = array(
    'post_type' => 'event',
    'posts_per_page' => '-1',
    'post_status' => array( 'private','publish' ),

    'meta_key' =>'start_date',
    'orderby' => 'meta_value_num',
    'order' => 'asc',

    'meta_query' => array(
        'relation' => 'OR',
            'key' => 'end_date',
            'value' => array(strtotime('20130101'),strtotime('20140101')),
            'compare' => 'BETWEEN',
            'type' => 'NUMERIC'
            'key' => 'start_date',
            'value' => array(strtotime('20130101'),strtotime('20140101')),
            'compare' => 'BETWEEN',
            'type' => 'NUMERIC'
$search = new WP_Query($args);

if ( $search->have_posts() ):

    setlocale(LC_ALL, 'nl_NL.UTF-8');

    while ( $search->have_posts() ) : $search->the_post();

        $start_date = get_post_meta(get_the_ID(), 'start_date', true);
        $end_date = get_post_meta(get_the_ID(), 'end_date', true);
        $start = strftime( '%A %e %B %Y', strtotime( $start_date ) );
        $end = strftime( '%A %e %B %Y', strtotime( $end_date ) );

        echo "$start ($start_date) - $end ($end_date) <br />";


See here also: http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

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.