mysql - WP 查询以列出在两个特定日期之间发布的帖子

标签 mysql wordpress

我只想列出两个特定日期之间发布的帖子

我在这里找到了解决方案 http://www.wprecipes.com/wordpress-loop-get-posts-published-between-two-particular-dates

我的代码在这里,不幸的是这不起作用!!

<?php
          function filter_where($where = '') {
                $where .= " AND post_date >= '2012-05-09' AND post_date <= '2012-05-11'";
                //$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
            return $where;
          }
        add_filter('posts_where', 'filter_where');
        query_posts($query_string);
        ?>
               <?php while (have_posts()) : the_post(); ?>    
                    <h1><?php the_title(); ?></h1>
               <?php endwhile; ?>

最佳答案

首先你不应该使用 query_posts() 因为它会破坏全局并弄乱条件等...

您没有提到这段代码在哪里运行。例如,如果它在页面模板上,您将不会收到任何帖子,因为当将 $query_string 传递到您的查询中时,您使用的是全局 $wp_query 对象,它只会查询页面 post_types。

我使用新的 WP_Query 而不是 query_posts 测试了您的代码,它运行良好。

function filter_where( $where = '' ) {

$where .= " AND post_date >= '2012-05-01' AND post_date < '2012-05-14'";
return $where;
}

add_filter( 'posts_where', 'filter_where' );

$query = new WP_Query( array( 'post_type' => 'post' ) );

    echo '<div id="content" style="color: white!important;">';
    echo '<h1>And here are the posts……</h1>';
    while( $query->have_posts() ) : $query->the_post();
    
    echo '<h2>'. get_the_title() .'</h2>';
    
    
    endwhile;
    echo '</div>';
    
    
remove_filter( 'posts_where', 'filter_where' );

关于mysql - WP 查询以列出在两个特定日期之间发布的帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10578032/

相关文章:

mysql - 在mysql foreach中转换为Markdown并添加到数组

MYSQL:根据另外两个表的连接计算一个表中的行数

php - 数据库设计

javascript - 我的子菜单在 WordPress 中悬停时未显示

wordpress 多站点问题

Mysql Convert_tz

java - MySQL CallableStatement.getObject 具有不一致的行为

css - wp_bootstrap_navwalker 子菜单点击或悬停不显示子菜单

wordpress - 将 WordPress 本地迁移到实时,图像链接指向本地主机 - 无法纠正

css - 样式子菜单 CSS