简体   繁体   中英

WordPress Query display only posts modified today

I'm trying to create a query in WordPress that displays only the posts that were edited today, excluding those posted today. I've tried several variations but nothings seem to be working:

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

I'm not quite sure what to put in key , although that isn't the only problem.

Its not the best solution but you can just do the filter after your query and check if the current date string is inside the post date modified,

eg

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

If I get it right, with "displays only the posts that were edited today, excluding those posted today."

I guess you mean display ONLY old published posts modified/edited today.

If that is the case, this might help you:

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

Based on this query to select all posts either published or modified today , you could just write this WP_Query to retrieve only the modified ones:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM