使用帖子的发布日期搜索帖子

时间:2011-12-21 作者:Pete

我想根据发布日期搜索帖子。例如,在搜索框中,我会将2011年11月22日放在那里,并显示2011年11月22日期间的所有帖子。我非常想知道怎么做。任何帮助都将不胜感激。我正在使用一个主题,它是搜索。php是这样的。我可以插入您对此搜索的查询吗。php?由于我是wordpress的初学者,我不知道把你给的代码放在哪里。非常感谢您抽出时间。

<?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

2 个回复
最合适的回答,由SO网友:Rob Vermeer 整理而成

您可以为此特定搜索页面创建自定义页面模板http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates.

然后设置一些$\\u GET变量(使用带有GET方法的表单)。

并对这些$\\u GET值执行自定义查询:

$date_query = new WP_Query( \'year=\' . $_GET[\'year\'] . \'&monthnum=\' . $_GET[\'month\'] . \'&day=\' . $_GET[\'day\'] );
然后自定义循环:

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

很抱歉花了这么长时间,有点忙。

以下是您的问题的解决方案:

把这个放在你的函数中。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);
                if($year)
                    $y = $year[0];
                preg_match(\'/^[0-3]{0,1}[0-9]{1} /\', $search_query, $day);
                if($day)
                    $d = $day[0];
            }
        }

        if(isset($d) && isset($m) && isset($y)) {
            $wd = explode($y, $search_query);
            if($wd[1])
                $query_string = \'s=\' . trim($wd[1]) . \'&year=\' . $y . \'&monthnum=\' . $m . \'&day=\' . $d;
            else
            $query_string = \'year=\' . $y . \'&monthnum=\' . $m . \'&day=\' . $d;
            query_posts($query_string);
        }
    }
}
add_action(\'get_header\', \'my_date_search\');
如果你在类似“2011年6月14日”的页面上搜索,它将显示2011年6月14日的搜索页面。如果您在“2011年6月14日searchthis”上搜索,它将显示2011年6月14日的搜索页面,其中包含搜索短语“searchthis”。

也许有一个更简单/更好的解决方案适合您,但这会奏效。

SO网友:mor7ifer

您应该能够使用WP\\u Query的时间参数,并且只需很少的代码即可完成此操作。退房the documentation. 此WP\\U查询将替换搜索中的循环。php。您应该可以灵活地添加其他参数,以获取要筛选的任何其他搜索结果。

结束

相关推荐

search only pages if on page

我有一个由网页和帖子组成的网站。只有几页和大量的帖子。如何将侧边栏中的搜索框设置为仅在页面上搜索页面,或仅在博客页面上搜索帖子?现在它将我搜索页面上的所有内容混合在一起。如果能在搜索框下添加复选框来选择帖子或页面,那就更酷了。谢谢