簡體   English   中英

在Wordpress中,我可以將WP_Query與時間戳一起使用嗎?

[英]In Wordpress, can I use WP_Query with timestamp?

我有這樣的代碼,

    $date_arg = array(
        'after' => array(
            'year'  => $num_days_ago['year'],
            'month' => $num_days_ago['mon'],
            'day'   => $num_days_ago['mday'],
    ),
        'inclusive' => false,
    );
    $query = new WP_Query( array ( 'date_query' => $date_arg, 'post_type' => $post_type ...

這個工作正常,但我必須指定一個確切的日期。 是否可以使用“時間戳”進行查詢。

例如,我要查詢24小時內的帖子。 我可以通過獲得時間戳

$ts = time() - DAY_IN_SECONDS

是否有可能獲得在$ ts之后創建的帖子?

您可以嘗試使用date()strtotime()函數構造日期。

$date_query = array(
    'after' => date('Y-m-d H:i:s', strtotime('-24 hours'))
);

我還建議您瀏覽這個方便的網站-http: //www.viper007bond.com/tag/wp_date_query/

恕我直言,像這樣更好:

function filter_where($where = '') { 
  $where .= " AND post_date > '" . date('Y-m-d H:i:s', strtotime('-24 hours')) . "'"; 
  return $where; 
} 
add_filter('posts_where', 'filter_where');

更一般的:

// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
    // posts in the last x days
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";// here 30
    return $where;
}

add_filter( 'posts_where', 'filter_where' ); // add the filter before setting object or before loop
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' ); // .. and do not forget to remove it 

或者你可以嘗試這樣的事情

SELECT post_title, IF (post_date_gmt < (DATE_SUB(CURDATE(), INTERVAL 24 HOUR)), 'false', 'true') AS A FROM wp_posts
WHERE post_type = "post" AND post_status = "publish"

http://www.w3schools.com/sql/func_date_sub.asp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM