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 am making a search form that searches for posts within two meta box values and display results between them.

I have this set

$prefix = 'ghes_'; // start with an underscore to hide fields from custom fields list
add_filter( 'ghes_meta_boxes', 'ghes_sample_metaboxes' );
function ghes_sample_metaboxes( $meta_boxes ) {
    global $prefix;
    $meta_boxes[] = array(
        'id' => 'event_meta',
        'title' => 'Event Metabox',
        'pages' => array('event'), // post type
        'context' => 'normal',
        'priority' => 'high',
        'show_names' => true, // Show field names on the left
        'fields' => array(

        array(
                'name' => 'Event Start Date',
                'desc' => 'field description (optional)',
                'id' => $prefix . 'event_start_timestamp',
                'type' => 'text_date_timestamp'
            ),                  
            array(
                'name' => 'Event End Date',
                'desc' => 'field description (optional)',
                'id' => $prefix . 'event_end_timestamp',
                'type' => 'text_date_timestamp'
            )                   

How will the query look like if I want to search for posts between the "Event Start Date" and the "Event End Date".

Thanks.

share|improve this question
add comment

1 Answer

You will need to use the meta_query variable like so:

$start = time(); //Now
$end= strtotime('+1 month'); //1 month ahead

query_posts(
    'post_type' => 'event',
    'post_status' => 'publish',
    'posts_per_page' => '10',
    'meta_query' => array(
        array('key' => $prefix.'event_start_timestamp', 'value' => $start, 'compare' => '>=', 'type' => 'NUMERIC'),
        array('key' => $prefix.'event_end_timestamp', 'value' => $end, 'compare' => '<=', 'type' => 'NUMERIC')
    )
);
share|improve this answer
    
Okay, I have this as my search form (custom-search.php): pastebin.com/vBzzmG83 and this is what I have for my search results page (search.php):pastebin.com/1Hv4s2qM When I perform a search, I get the "Nothing found" message even though I have posts with events happening between the dates I select. Maybe I am missing something here. The start and End dates of the events are saved as timestamps with the date format mm/dd/yyyy. I also tried pointing the form action like this <form action="<?php bloginfo('home'); ?>/search.php" method="GET"> and I hit a 404. Clues? –  Kwame Boame Dec 14 '11 at 1:00
    
if the timestamp is saved in the db like mm/dd/yyyy change 'type' => 'NUMERIC' to 'type' => 'DATE' –  Ijaas Dec 14 '11 at 10:39
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.