How to get current page ID in WordPress shortcode for dynamic filtering

I’m working with WordPress shortcodes and trying to make them more dynamic. Right now I have a data table that connects to a Google Sheet. The person managing the sheet isn’t technical so I want to keep things simple for them.

Currently I’m using this shortcode:
[data_table id="892" search=0 filter="2200" filter_column="Category" columns="title,location,phone,email,social"]

The problem is I have to manually add this shortcode to every post and change the filter value each time. Instead of hardcoding “2200” as the filter value, I want it to automatically use the current post’s ID. This way I can put the shortcode in my template once and it will work everywhere.

My plan is to add the post IDs to the Google Sheet which is much easier to manage. I’ve seen some PHP solutions but I’m not a developer and want to avoid complex coding. Is there a way to use variables in shortcodes that can grab the current page information automatically?

WordPress has get_the_ID() to grab the current post ID, but shortcodes don’t support this out of the box. Most shortcode plugins let you modify their behavior through hooks though. You’ll want to register a custom version of your existing shortcode that automatically pulls the post ID. Just add a wrapper function to your theme’s functions.php that gets the current post ID and passes it to your original shortcode. This keeps your Google Sheet management simple and you won’t have to manually update filter values anymore. The code’s only about 5-10 lines and you add it once. Your new shortcode would be something like [dynamic_data_table] with no filter parameter needed.

Most shortcode plugins don’t handle dynamic variables by default, but there’s an easier way to tackle this. Instead of messing with the shortcode itself, create a custom field for each post to store the filter value. Then modify your shortcode to accept something like filter="{custom_field}" if your plugin supports it. You should also check your data table plugin’s docs - some let you use WordPress functions directly in shortcodes. If you’re okay with basic PHP, you could build a wrapper shortcode that automatically feeds the post ID to your existing one. Just means adding a few lines to your theme’s functions.php file - no complex coding needed.

hey, have you tried using shortcodes with WP variables? like {post_id} or something similar, they might make it easier for you. if that doesn’t work, you might have to tweak functions.php but idk if that’s your speed.