-1

我在两个日期之间创建了自定义帖子搜索。它的工作和显示结果,但有一个问题。它没有显示确切的搜索结果。

例如:如果我将 20-3-2013 添加到 27-3-2013 ,它会显示其他日期和月份的有线结果。它完全没有得到确切的价值。

这是我的代码。php

<?php
function getCatSearchFilter($pre,$post){
$category = "";
$catId = htmlspecialchars($_GET["cat"]);
if ($catId != null && $catId != '' && $catId != '0'){
$category = $pre.get_cat_name($catId).$post;
}
return $category;
}
?>
    <?php
        $search_word="";

        if(!empty($_REQUEST['s'])){
            $name_word=$_REQUEST['s'];
            $search_word="(post_title LIKE \"%$name_word%\" or post_content LIKE \"%$name_word%\") and ";
        }
        //if(!empty($_REQUEST['d']) && $_REQUEST['d'] != 'von'){
        if(!empty($_REQUEST['sDate'])){
            $s_name_date=$_REQUEST['sDate'];
            $s_custom_date1 = substr($s_name_date,0,2);
            $s_custom_date2 = substr($s_name_date,3,2);
            $s_custom_date3 = substr($s_name_date,6,4);
            $s_name_date = "$s_custom_date3.$s_custom_date2.$s_custom_date1";
            //$search_word.="post_date LIKE \"%$name_date%\" and ";
                        if(!empty($_REQUEST['eDate'])){
                            $e_name_date=$_REQUEST['eDate'];
                            $e_custom_date1 = substr($e_name_date,0,2);
                            $e_custom_date2 = substr($e_name_date,3,2);
                            $e_custom_date3 = substr($e_name_date,6,4);
                            $e_name_date = "$e_custom_date3.$e_custom_date2.$e_custom_date1";
                            //$where .= " AND post_date >= '$s_name_date' AND post_date < '$e_name_date'";
                            $where .= "AND pm.meta_key='custom_time_from' and pm.meta_value >= '$s_name_date'
and pm.meta_value < '$e_name_date'";
                        }
        }
        if($_REQUEST['cat']){
            $categoryId = $_REQUEST['cat'];
            $search_word.="ID in (select p.ID from $wpdb->terms c,$wpdb->term_taxonomy tt,$wpdb->term_relationships tr,$wpdb->posts p ,$wpdb->postmeta t where c.term_id like '".$categoryId."' and c.term_id=tt.term_id and tt.term_taxonomy_id=tr.term_taxonomy_id and tr.object_id=p.ID and p.ID = t.post_id and p.post_status = 'publish' group by p.ID) and";
        }
        if($where)
        {       
        $query ="select p.* from wp_posts p 
join wp_postmeta pm on p.ID=pm.post_id
WHERE p.post_status='publish' and p.post_type='post' $where";   
}
else
{
        $query="select * from wp_posts WHERE $search_word post_status='publish'";
}

    $pageposts=$wpdb->get_results($query, OBJECT);

    if ($pageposts):
    global $post;
    foreach ($pageposts as $post):
    setup_postdata($post);
    ?>
    <?php if($post->post_type=='post')
    {
    ?>

这是html

        <div id="search">
        <form method="get" id="searchform" action="" >
        <input id="s" style="" type="text" name="s" placeholder="search" value="" />
        <input style="width:95px; " type="text" name="sDate" class="datepicker" placeholder="from" />
        <input style="width:95px;" type="text" name="eDate" class="datepicker" placeholder="to" />

请帮助完成这项工作。

提前致谢 :)

4

1 回答 1

-1
// Set arguments for events
$start = '2011-11-31';
$end = '2011-10-01';
$args = array(
    'post_type' => 'my-event-type',
    'posts_per_page' => -1,
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'meta_key' => '_my-datetime-from',
    'meta_query' => array(
        array(
            'key' => '_my-datetime-from',
            'value' => array($start, $end),
            'compare' => 'BETWEEN',
            'type' => 'DATE'
        )
    )
);
// Make the query
$events_query = new WP_query();
$events_query->query($args);

取自以下链接

https://wordpress.stackexchange.com/questions/34888/how-do-i-search-events-between-two-set-dates-inside-wp

于 2013-09-08T19:02:49.790 回答