0

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.

1 Answer 1

2

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')
    )
);
2
  • 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? Dec 14, 2011 at 1:00
  • if the timestamp is saved in the db like mm/dd/yyyy change 'type' => 'NUMERIC' to 'type' => 'DATE'
    – Ijaas
    Dec 14, 2011 at 10:39

Your Answer

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

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