dorv56831 2012-06-01 13:44
浏览 26
已采纳

Wordpress高级搜索

I had created a custom search page containing the code

<?php 
function filter_where( $where = '' ) {
    $where .= " AND post_date >= '{$_GET['frmdate']}' AND post_date < '{$_GET['todate']}'";
    return $where;
}
add_filter( 'posts_where', 'filter_where');
$query = new WP_Query( array( 'post_type' => 'post' ) );
    while( $query->have_posts() ) : $query->the_post(); ?>

    <p><?php the_title(); the_date() ?></p>

<?php    endwhile; ?>
<?php remove_filter( 'posts_where', 'filter_where' ); wp_reset_query(); ?>

My Aim is Search posts with 'keywords' between 'Two Dates'

my search variables passing through url like this

www.something.com?page_id=372&s12=lorem&s13=ipsum&frmdate=2012-03-01&todate=2012-06-12

with my Current code to filter the result between two date is Working perfectly when my url is looks like this www.something.com?page_id=372&frmdate=2012-03-01&todate=2012-06-12

when iam adding &s=lorem isn't work both keyword and date filtering. even I add it (&s=lorem) alone

  • 写回答

2条回答 默认 最新

  • douai2499 2012-06-05 06:28
    关注

    Updated My code with the below one its working fine with my all requirements, Not sure if there is an another smarter way

    <?php 
    function filter_where( $where = '' ) {
        if(isset($_GET['frmdate']) && isset($_GET['todate'])){
            $where .= " AND post_date >= '{$_GET['frmdate']}' AND post_date < '{$_GET['todate']}'";
        }
        if(isset($_GET['s12'])){
            $where .= " AND post_content LIKE '%{$_GET['s12']}%' " ;
        }
        if(isset($_GET['s13'])){
            $where .= " AND post_title LIKE '%{$_GET['s13']}%' "    ;
        }
        //echo $where;exit;
        return $where;
    }
    add_filter( 'posts_where', 'filter_where');
    $query = new WP_Query( array( 'post_type' => 'post' ) );
    
        while( $query->have_posts() ) : $query->the_post(); ?>
    
        <li>
            <h4><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
            <div class="monthDate"> <span class="date"><?php the_time('M d,Y H.i'); ?></span> <span class="month"><?php comments_number();?></span> </div>
            <p><?php content('100'); ?></p>
        </li>
    
    <?php    endwhile; ?>
    <?php remove_filter( 'posts_where', 'filter_where' ); wp_reset_query(); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?