WordPress filter posts between two custom field dates

I have really searched around for an answer to this and not much comes close unless im missing something obvious so any help or even pointers on where to get started on this would be much appreciated

OK

Read More

I have set up a custom post type named “job_listing” containing two custom fields named “dateFrom” “dateTo”

I want to be able to filter these posts using a form with two fields named
“dateFrom” “dateTo”

If form fields entered values are within the range of a posts “dateFrom” “dateTo” then the post should be displayed

Setting up the form is no problem but how to query between two custom field values has me well and truly stumped

Edited##

OK so this is what ive got so far

Below is the form, currently i’m manually inputting dates in this format 24/11/2013
I plan to use calendar input field but i can do that bit later

<form method="get" action="<?php bloginfo('url'); ?>/">
<input type="hidden" name="post_type" value="job_listing" />
<input name="search_dateFrom" id="search_dateFrom" onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'Search':this.value;" value="Search Date From" />
<input name="search_dateTo" id="search_dateTo" onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'Search':this.value;" value="Search Date To" />
<input type="submit" name="submit" />
</form>

Below is the custom results template, as you can see im trying to display results from the post_type “job_listing” and then trying to filter only posts that have a custom field start date and end date within the range of the form input

<?php 
$args = array(
    'post_type' => array('job_listing'),
    'post_status' => 'publish',
);
$args['meta_query'][] = array(
    'key' => 'dateFrom',
    'value' => $s,
    'compare' => 'LIKE',
);

$args['meta_query'][] = array(
    'key' => 'dateTo',
    'value' => $s,
    'compare' => 'LIKE',
);

$a = strtotime($custom['dateFrom']); 
$b = strtotime($custom['dateTo']); 
$x = strtotime($time_from_form);

if( $x > $a && $x < $b ) {$query = new WP_Query( $args );
if ( $query->have_posts() ): while ( $query->have_posts() ) : $query->the_post(); 
?>

<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
<?php } ?>

Im stumped, any ideas where im going wrong, Thanks for your time guys
DW

Related posts

Leave a Reply

1 comment

  1. You could use strtotime to convert the dates into seconds, so you’d be working with something like this:

    if ( strtotime($time_from_form)) >= strtotime($custom['availableDateStart']) && $strtotime($time_from_form) <= strtotime($custom['availableDateEnd']) ) {
        //do your stuff
    }
    

    Basically, this will convert all 3 dates to seconds. So it would be like

    $a = strtotime($custom['availableDateStart']); // 12345
    $b = strtotime($custom['availableDateEnd']); // 54321
    $x = strtotime($time_from_form); // 3333
    
    if( $x > $a && $x < $b ) {
        //do your stuff
    }
    

    Hope that helps 🙂