I want to search a post based on the date it was posted. Example, in the search box, I would place there November 22, 2011 and all posts during November 22, 2011 would be displayed. Im desperate on how to do this. Any help would be very much appreciated. I am using a theme and it's search.php goes something like this. Can I just insert that query you've given to this search.php? I don't know where to put those codes you've given since Im a beginner in wordpress. Thank you very much for your time.

<?php the_search_query(); ?>
<?php if (have_posts()) : while (have_posts()) :the_post(); 
$arc_year = get_the_time('Y'); 
$arc_month = get_the_time('m'); 
$arc_day = get_the_time('d'); 
<?php the_permalink(); ?>    
<?php the_title();
<?php the_time('F j, Y'); ?> at <?php the_time('g:i a'); ?>
<?php dp_attachment_image($post->ID, 'thumbnail', 'alt="' . $post->post_title . '" class="thumbnail"'); ?>
<?php the_excerpt(); ?>
<?php the_permalink(); ?>">READ MORE

You can make a custom page template for this specific search page http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates.

Then set some $_GET variables (using a form with method GET).

And do a custom query for these $_GET values:

$date_query = new WP_Query( 'year=' . $_GET['year'] . '&monthnum=' . $_GET['month'] . '&day=' . $_GET['day'] );

Then your custom loop:

<?php if ($date_query->have_posts()) : while ($date_query->have_posts()) : $date_query->the_post();?>
   <?php the_title(); ?>
<?php endwhile; endif; ?>


Sorry that it took so long, a bit busy.

Here is a solution for your problem:

put this in your function.php

function my_date_search() {
    if(is_search()) {
        $search_query = get_search_query();
        $months = array( 1 => "January", 2 => "February", 3 => "March", 4 => "April", 5 => "May", 6 => "June", 7 => "July", 8 => "August", 9 => "September", 10 => "October", 11 => "November", 12 => "December" );

        foreach($months as $month => $month_name) {
            if(stristr($search_query, $month_name)) {
                $m = $month;
                preg_match('/(19[0-9][0-9]|20[0-9][0-9])/', $search_query, $year);
                    $y = $year[0];
                preg_match('/^[0-3]{0,1}[0-9]{1} /', $search_query, $day);
                    $d = $day[0];

        if(isset($d) && isset($m) && isset($y)) {
            $wd = explode($y, $search_query);
                $query_string = 's=' . trim($wd[1]) . '&year=' . $y . '&monthnum=' . $m . '&day=' . $d;
            $query_string = 'year=' . $y . '&monthnum=' . $m . '&day=' . $d;
add_action('get_header', 'my_date_search');

If you search on something like "14 june 2011" than it will show a search page of 14 june 2011. If you search on "14 june 2011 searchthis" than it will show a search page for 14 june 2011 with the searchphrase "searchthis".

Maybe there is an easier/better solution for you, but this will work.

  • Please edit your original question and post there your code in code blocks (for readability). Jan 4 '12 at 16:05
  • I edited my original question. I placed my code there. Thanks again
    – Pete
    Jan 6 '12 at 15:19
  • Updated my posts. Jan 11 '12 at 22:25
  • Thank you sir! that was amazing! Can I ask you again another question? I have a theme installed here, but my problem is that the theme doesn't provide a next page function. So if I have 20 posts, and I put 10 in the Blog pages show at most in Reading Setting of Wordpress Setting, it only shows the 10 posts and then I can't anymore see the remaining posts on my home because it doesn't have any next button. I want to have a next page button so that the users can still view the other posts by clicking the next button. How can I do this?
    – Pete
    Feb 6 '12 at 13:33
  • Please accept the question so the system knows it has been answered. See the FAQ: wordpress.stackexchange.com/faq#howtoask Feb 6 '12 at 14:12

You should be able to use WP_Query's time parameters and do this with very little code. Check out the documentation. This WP_Query will replace the loop that is in search.php. You should have the flexibility to add additional args to get any other search results you want to filter for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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