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.

Show/hide block accordion to post date

  • I have a query loop and inside I have a headline block with the class of new-article. I have written some code to hide that block when the post associated with that block is more than 24 hours:

    add_filter('render_block', function ($block_content, $block) {
        // Check if the block has the class "new-article"
        if (!is_admin() && !empty($block['attrs']['className']) && strpos($block['attrs']['className'], 'new-article') !== false) {
            
            // Check if 'data' and 'postId' keys exist
    
                // Get the post ID associated with the block
                $post_id = $block['attrs']['data']['postId'];
    
                // Check if the post is older than 24 hours
                $post_date = get_the_time('U', $post_id);
                $current_time = current_time('timestamp');
                $time_difference = $current_time - $post_date;
    
                // If the post is older than 24 hours, hide the block
                if ($time_difference >= 86400) { // 86400 seconds = 24 hours
                    return '';
                }
    
        }
    
        return $block_content;
    }, 10, 2);

    It works, but I get a PHP warning:
    Warning: Undefined array key "data" in /home/[sitename]/[siteURL]/wp-content/themes/theme-name/functions.php on line 149

    Line 149 is: $post_id = $block['attrs']['data']['postId'];

    I tried adding a check:

    add_filter('render_block', function ($block_content, $block) {
        // Check if the block has the class "new-article"
        if (!is_admin() && !empty($block['attrs']['className']) && strpos($block['attrs']['className'], 'new-article') !== false) {
            
            // Check if 'data' and 'postId' keys exist
            if (isset($block['attrs']['data']) && isset($block['attrs']['data']['postId'])) {
    
                // Get the post ID associated with the block
                $post_id = $block['attrs']['data']['postId'];
    
                // Check if the post is older than 24 hours
                $post_date = get_the_time('U', $post_id);
                $current_time = current_time('timestamp');
                $time_difference = $current_time - $post_date;
    
                // If the post is older than 24 hours, hide the block
                if ($time_difference >= 86400) { // 86400 seconds = 24 hours
                    return '';
                }
            }
        }
    
        return $block_content;
    }, 10, 2);

    But now the headline block always shows. What am I doing wrong?

  • Hi George,

    Why not use $post_id = get_theID(); instead of $post_id = $block['attrs']['data']['postId'];?

  • Doing that is not working as intended because the headline block always shows.

    Sorry, thread title should have been: Show/hide block according to post date!

  • It should not, i just tested on my site and it works.

  • I get Uncaught Error: Call to undefined function get_theID() in wp-content/themes/theme_name/functions.php:152

  • Hi George,

    Sorry, there was a typo in Ying’s code. It should be get_the_ID().

    Can you try that instead?

  • Here is my code so far:

    add_filter('render_block', function ($block_content, $block) {
        // Check if the block has the class "new-article"
        if (!is_admin() && !empty($block['attrs']['className']) && strpos($block['attrs']['className'], 'new-article') !== false) {
            
            // Check if 'data' and 'postId' keys exist
            if (isset($block['attrs']['data']) && isset($block['attrs']['data']['postId'])) {
    
                // Get the post ID associated with the block
                $post_id = get_the_ID();
    
                // Check if the post is older than 24 hours
                $post_date = get_the_time('U', $post_id);
                $current_time = current_time('timestamp');
                $time_difference = $current_time - $post_date;
    
                // If the post is older than 24 hours, hide the block
                if ($time_difference >= 86400) { // 86400 seconds = 24 hours
                    return '';
                }
            }
        }
    
        return $block_content;
    }, 10, 2);

    The block is always visible, though.

  • Hi there,

    i think this is what Ying meant:

    add_filter('render_block', function ($block_content, $block) {
        // Check if the block has the class "new-article"
        if (
            !is_admin() && 
            !empty($block['attrs']['className']) && 
            strpos($block['attrs']['className'], 'new-article') !== false
        ) {
            
            // Get the post ID associated with the block
            $post_id = get_the_ID();
    
            // Check if the post is older than 24 hours
            $post_date = get_the_time('U', $post_id);
            $current_time = current_time('timestamp');
            $time_difference = $current_time - $post_date;
    
            // If the post is older than 24 hours, hide the block
            if ($time_difference >= 86400) { // 86400 seconds = 24 hours
                return '';
            }
    
        }
    
        return $block_content;
    }, 10, 2);
  • Yes, I see, it works, thanks!

  • You’re welcome

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