显示存档中的未来帖子

时间:2014-09-04 作者:descentropy

我使用以下代码来显示帖子的存档。

<?php
// Get years that have posts
$years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM wp_posts WHERE post_type = \'post\' AND post_status = \'publish\' GROUP BY year DESC" );

// For each year, do the following
foreach ( $years as $year ) {

    // Get all posts for the year
    $posts_this_year = $wpdb->get_results( "SELECT * FROM wp_posts WHERE post_type = \'post\' AND post_status = \'publish\' AND post_status = \'future\' AND YEAR(post_date) = \'" . $year->year . "\' ORDER BY post_date DESC" );

        foreach ( $posts_this_year as $post ) {
            // echoing miscellaneous stuff
        }
}
?>
上面写着post_status = \'publish\'. 如何将未来的帖子添加到存档中?我尝试添加AND post_status = \'future\' 对双方$wpdb->get_results 但它不起作用。

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

您需要使用“OR”而不是“AND”,

<?php
// Get years that have posts
$years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM wp_posts WHERE post_type = \'post\' AND (post_status = \'publish\' or post_status = \'future\') GROUP BY year DESC" );

// For each year, do the following
foreach ( $years as $year ) {

    // Get all posts for the year
    $posts_this_year = $wpdb->get_results( "SELECT * FROM wp_posts WHERE post_type = \'post\' AND (post_status = \'publish\' OR post_status = \'future\') AND YEAR(post_date) = \'" . $year->year . "\' ORDER BY post_date DESC" );

        foreach ( $posts_this_year as $post ) {
            // echoing miscellaneous stuff
        }
}
?>

SO网友:newplenty

这一款也适用:

function wpsites_query( $query ) {
if ( $query->is_archive() && $query->is_main_query() && !is_admin() ) {
    $query->set( \'post_status\', array(\'publish\',\'future\') );
  }
}
add_action( \'pre_get_posts\', \'wpsites_query\' );

结束

相关推荐

Hook into $wpdb

有没有办法连接到WPDB实例($WPDB)?我基本上想要的是在执行所需的查询get之前执行自己的函数。这可能吗?第一个答案的附加评论问题: $query = $wpdb->prepare(\"SELECT * FROM \" . $wpdb->prefix . \"test WHERE type = %s and ip = %s\", $type, $ip_full_log); $fetch = $wpdb->get_results($query, ARRAY_A)