【问题标题】:WordPress Query display only posts modified todayWordPress 查询仅显示今天修改的帖子
【发布时间】:2016-05-13 07:02:28
【问题描述】:

我正在尝试在 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 中输入什么,尽管这不是唯一的问题。

【问题讨论】:

  • 欢迎来到 SO。你的问题不清楚。您发布的代码到底有什么问题?请编辑问题并澄清。

标签: php wordpress querying


【解决方案1】:

如果我没记错的话,“只显示今天编辑的帖子,不包括今天发布的帖子。”

我猜你的意思是只显示今天修改/编辑的旧帖子。

如果是这样,这可能会对您有所帮助:

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

【讨论】:

  • 你好,我的朋友。是的,您的代码帮助我实现了所需,只是对其进行了很大的编辑。谢谢
【解决方案2】:

这不是最好的解决方案,但您可以在查询后进行过滤并检查当前日期字符串是否在修改的发布日期内,

例如

$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>';

【讨论】:

    【解决方案3】:

    根据对select all posts either published or modified today 的查询,您可以编写这个 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 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      相关资源
      最近更新 更多