簡體   English   中英

WordPress查詢僅顯示今天修改的帖子

[英]WordPress Query display only posts modified today

我正在嘗試在WordPress中創建一個查詢,該查詢僅顯示今天編輯的帖子, 不包括今天發布的帖子 我嘗試了幾種變體,但似乎沒有任何效果:

$today = current_time('Ymd');

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => '10',
    'meta_query' => array(
        array(
            'key' => 'modified',
            'compare' => '>=',
            'value' => $today,
            'type' => 'NUMERIC,'
            )
    ),
    'orderby' => 'modified',
    'order' => 'DESC',
    'ignore_sticky_posts' => '1'
);

我不太肯定要放什么key ,雖然這不是唯一的問題。

它不是最佳解決方案,但您可以在查詢后進行過濾,並檢查當前日期字符串是否在修改后的發布日期之內,

例如

$ar = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => '10',
    'orderby'   => 'modified',
    'order'     => 'DESC',
    'ignore_sticky_posts' => '1'
);
$q = new WP_QUery( $ar );
$p = $q->get_posts();
foreach( $p as $a ) {
    $c = current_time( 'Y-m-d' );
    if ( strpos( $a->post_modified, $c ) !== false ) {
        _e( $a->post_title .' '.$a->post_modified. ' - ' . $c. "<br>" );
    }
} 
#echo '<pre>', print_r($p, 1), '</pre>';

如果我做對了,請使用“僅顯示今天編輯的帖子,不包括今天發布的帖子”。

我想你的意思是只顯示今天修改/編輯的舊發布帖子。

如果是這種情況,這可能對您有幫助:

<?php
    // query args
    $args = array(
            'posts_per_page'        => '10',
            'post_type'             => 'post',
            'post_status'           => 'publish',
            'orderby'               => 'modified',
            'order'                 => 'DESC',
            'ignore_sticky_posts'   => '1',
            'caller_get_posts'      => 1
    );

    // query
    $updated = new WP_Query($args);

    // loop
    while($updated->have_posts()) : $updated->the_post(); 

    $today = current_time('Y-m-d'); // current date a.k.a. TODAY
    $pub = get_the_time('Y-m-d', $updated->ID); // date when post was published
    $mod = get_the_modified_time('Y-m-d', $updated->ID); // date when post was last modified

    // if post NOT published today AND was modified today display: 
    if ( $pub !== $today && $mod === $today ) :
?>

<!-- here goes your normal wp game -->
<h1><?php the_title ?></h1>
<span><?php the_date(); ?></span>
<p><?php the_excerpt(); ?></p>

<?php endif; endwhile; ?>

基於此查詢, 選擇所有今天發布或修改的帖子 ,您只需編寫此WP_Query即可僅檢索修改后的帖子:

   $args = array(
        'post_type' => 'post',
        'post_status' => 'any', // we also want the drafts
        'nopaging'=>true,
        'date_query' => array(
            'column' => 'post_modified',
            'year'  => $day_parsed['year'],
            'month' => $day_parsed['month'],
            'day'   => $day_parsed['day'],
        )
    );

    $query_day_posts = new WP_Query( $args );

暫無
暫無

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

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