Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Query loop with last modified date also shows new posts

  • Hi there,
    I want a query loop in the sidebar that only shows updated posts.
    I have set this: Sort by: last modified date
    However, it is now the case that new posts that are published are also displayed. They have not been updated but are still displayed.
    How can I ensure that only updated posts are displayed?
    Thank you very much for your help.

  • Hi there,

    WordPress saves the a modified date for all new posts, it just happens to be the same as published date.
    So to exclude all new posts, you need to tell the WP Query to exclude any posts where the modified and published dates match.

    To do that in a GB Query Loop block:

    1. Add this PHP Snippet to the site:

    
    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
        if ( 
            ! is_admin() && 
            ! empty( $attributes['className'] ) && 
            strpos( $attributes['className'], 'mod-post-only' ) !== false 
        ) {
            $query_args = array_merge( $query_args, array(
                'date_query'     => array(
                    array(
                        'column'  => 'post_modified',
                        'after'   => 'post_date',
                    ),
                ),    
            ));
        }
        return $query_args;
    }, 10, 2 );
    

    2. select the Grid Block inside the Query Loop
    2.1 in the block settings Advanced > Additional CSS Class(es) add: mod-post-only

  • Hi David,

    thanks for your explanation – that makes sense.

    I copied the code into my function.php in the child theme and added the CSS class to the Grid Block.
    But it does not work. New posts are still listed (I have double checked the implementation).

    Any new ideas?

  • Yeah, to be honest, it was some potluck shooting on my part here…. and looking at it now, was a bit dumb as the after arg requires an actual date.

    So… not 100% sure on this but you could try:

    1. Remove thea above snippet

    2. add this snippet to create a custom query arg of: exclude_unmodified

    
    add_filter( 'posts_where', 'exclude_unmodified_posts_where', 10, 2 );
    
    function exclude_unmodified_posts_where( $where, $query ) {
        global $wpdb;
    
        if ( $query->get( 'exclude_unmodified' ) ) {
            $where .= " AND {$wpdb->posts}.post_modified != {$wpdb->posts}.post_date";
        }
    
        return $where;
    }
    

    3. Add this snippet to write in our new arg.

    
    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
        if ( 
            ! is_admin() && 
            ! empty( $attributes['className'] ) && 
            strpos( $attributes['className'], 'mod-post-only' ) !== false 
        ) {
            $query_args = array_merge( $query_args, array(
                'exclude_unmodified' => true,    
            ));
        }
        return $query_args;
    }, 10, 2 );
    
    

    4. as above add the mod-post-only class to the query loops grid block.

  • Thanks David,
    your new snippets work – really good work, thank you very much.
    Best support!

    Have a nice day and greetings from germany 🙂

  • Awesome – glad to hear that worked!

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.