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.

Restrict loop results by post age

  • Hello,

    I’d like to create a loop and show only posts that are 30 days old. Is there an easy way to realize this?
    I think the standard only allows before and after date but not the “age” of a post.

    Thank you and best regards,
    Tim

  • Hi there,

    The query loop block does not have this parameter.

    You will need to write custom PHP code to add the argument, try below PHP code, and add posts-in-30-days class to the Grid block of the query loop block.

    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
        if (
            ! empty( $attributes['className'] ) &&
            strpos( $attributes['className'], 'posts-in-30-days' ) !== false &&
            ! is_admin()
        ) {
            // Set the date query to only include posts published within the last 30 days
            $query_args['date_query'] = array(
                array(
                    'after' => '30 days ago',
                ),
            );
        }
    
        return $query_args;
    }, 10, 2 );

    Adding PHP: https://docs.generatepress.com/article/adding-php/

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