WordPress.org

Forums

Custom search filters between 2 dates (5 posts)

  1. fafa-webwau
    Member
    Posted 2 years ago #

    Hi everybody,

    I'm developing a WordPress Theme and I created a pages that shows a category of posts with a search filter on top of that page.
    The search filter should give the possibility the visitors to show posts between 2 dates.
    For this, I included in the page the main search form and I modified it like this:

    <form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>news-press-release/">
    		<label for="s" class="assistive-text">DATE SEARCH</label>
    		<input type="text" class="field" name="s" id="s" placeholder="<?php esc_attr_e( 'Search', 'twentyeleven' ); ?>" />
            <label for="from_day" class="from">from</label>
            <select name="from_day">
                <option>DD</option>
                <option>1</option>
                <option>2</option>
    ...
            </select>
            <select name="from_month">
                <option>MM</option>
                <option>1</option>
                <option>2</option>
    ...
            </select>
            <select name="from_year">
                <option>YY</option>
                <option>2012</option>
                <option>2013</option>
            </select>
            <label for="to_day" class="from">to</label>
            <select name="to_day">
                <option>DD</option>
                <option>1</option>
                <option>2</option>
                <option>3</option>
                <option>4</option>
    ...
            </select>
            <select name="to_month">
                <option>MM</option>
                <option>1</option>
                <option>2</option>
    ...
            </select>
            <select name="to_year">
                <option>YY</option>
                <option>2012</option>
                <option>2013</option>
            </select>
            <input type="submit" class="submit" name="submit" id="searchsubmit" value="Go" />
    	</form>

    As you can see, the form is redirecting to the same page I use to show the posts.
    To show the posts, I use this code in the template page:

    <?php query_posts('category_name=news&posts_per_page=10&order=ASC'); ?>
                        <?php
                        if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
                            <div class="post" id="post-<?php the_ID();?>">
                                <p class="postmetadata">
                                        <?php the_time('j F Y') ?>
                                </p>
                                <p class="news_title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
                                <?php the_excerpt(); ?>
                            </div><!--end post-->
                        <?php endwhile; ?>
                        <?php endif; ?>

    I tried to add a where filter the before the loop but it always shows me the same posts list whatever the date I choose.

    Does anybody have an idea of what I made wrong ?

    Thank you

  2. Andrew Bartel
    Member
    Posted 2 years ago #

    Sorry maybe I missed it, but you're not passing a date parameter to query_posts() right...?

    This would be an instance where you should use WP_Query instead of modifying the main query with query_posts().

    Check out the time parameters for WP_Query.

    If you give it a shot and it's still not working repost the code that you've tried and we'll work through debugging it.

  3. fafa-webwau
    Member
    Posted 2 years ago #

    Hi Andrew Bartel,
    Thanks for taking the time to analyse and answer my question.

    I tried to use WP_Query several ways:

    <?php wp_reset_query(); ?>
                        <?php query_posts('category_name=news&posts_per_page=5&&order=ASC'); ?>
                        <?php
                         function filter_where( $where = '' ) {
    	                    // posts for March 1 to March 15, 2010
    	                    $where .= " AND post_date >= '2010-03-01' AND post_date < '2010-03-16'";
    	                    return $where;
                        }
    
                        add_filter( 'posts_where', 'filter_where' );
                        $query = new WP_Query( $query_string);
                        remove_filter( 'posts_where', 'filter_where' );
                        ?>

    (Just adding it after the filer)
    Nothing changes even if I change the dates.

    I tried to remove the query_posts but when I do this, I don't get any result (I got the current page link instead of the posts list).
    So I tried to remove the query post and add my query inside the wp_query like this

    <?php wp_reset_query(); ?>
                        <?php
                         function filter_where( $where = '' ) {
    	                    // posts for March 1 to March 15, 2010
    	                    $where .= " AND post_date >= '2010-03-01' AND post_date < '2010-03-16'";
    	                    return $where;
                        }
    
                        add_filter( 'posts_where', 'filter_where' );
                        $query = new WP_Query( $query_string . 'category_name=news&posts_per_page=5&&order=ASC' );
                        remove_filter( 'posts_where', 'filter_where' );
                        ?>

    And I got the same result.
    Do you have an idea ?

    (For the moment I don't use the form, I write the code directly in the theme template to see if the results are right.

  4. fafa-webwau
    Member
    Posted 2 years ago #

    up

  5. fafa-webwau
    Member
    Posted 2 years ago #

    Finally I found the good syntax and it works I can search posts between 2 dates through the form, but when I enter some text in the textfield, it redirects me to a "not found" page.

    Any idea ?

Topic Closed

This topic has been closed to new replies.

About this Topic