Hi there all.
I am developing a site for a fishing league. The site shows stats on anglers, fish clubs etc.
The site uses mainly wpdatatables pro and theme builders.
How it’s set up:
Each angler is a post of a custom post type (anglers) with a theme built to display on each “angler post”.
At the moment every value / stat on a “profile” (post) is its own sql table making use of the %CURRENT_POST_ID% placeholder.
i.e. SELECT ‘name’ FROM ‘angler dim’ WHERE ‘angler id’ = %CURRENT_POST_ID%
On the back end each ‘angler I’d’ is the same as their respective post ID so the correct value gets presented. Then the table gets displayed and the table is removed with CSS leaving the value.
Now this works but is quite horrible as every time someone loads a posts the database gets queried excessively and it adds LOADS of unnecessary css.
I want to create one big table of stats for all the anglers and store it in wp (will update it 8 times a year after competitions)
i.e.
Angler ID | Name | stat 1 | stat 2 | stat 3 |……….
12. Ben. 12. 8. 5
15. Don. 4. 7. 3
……………
Now i want to create a way to display a value depending on current post id
[display ‘stat1’ where ‘Angler ID’ = current post id]
wpdatatables has a single cell shortcode
[wpdatatable_cell table_id="1" column_key="column_name" column_id="custom_id_column" column_id_value="custom_id_column_value" sort="1"]But unfortunately this doesn’t work with the %CURRENT_POST_ID% placeholder.
Wpdatatables does make use of some variables var1 , var2 etc
So my question is there a way to make a CURRENT_POST_ID work with the single cell shortcode
Or set var1 to “current post id” with php and use var1 in shortcode.
Any suggestions to make this work will be highly appreciated.
If I’m going in the completely wrong direction in setting this up also feel free to point me the right way.
Or with advanced custom fields maybe i could store each stat in a field for each angler and use elementor dynamic tags to display each one. It’s just how would I automatically place the values in each field as there is 100s of anglers and it needs to be updated a couple times a year.
To streamline your process, using a single table for all anglers’ stats is a good approach. You can dynamically display values based on the current post ID by leveraging custom PHP and wpDataTables. Here’s a potential solution:
1. **Set up a global table**: As you’ve planned, create a single table in the database that includes all anglers and their respective stats.
2. **PHP and Shortcode**: Write a PHP function that fetches the `current post ID` and sets it as a global variable (e.g., `$var1`). Use this variable in your shortcode for wpDataTables:
“`php
function set_current_post_id_as_var1($atts) {
global $post;
return $post->ID;
}
add_shortcode(‘set_post_id’, ‘set_current_post_id_as_var1’);
“`
3. **Use the Shortcode in wpDataTables**: You can now use this variable (`var1`) in your wpDataTables shortcode like this:
“`plaintext
[wpdatatable_cell table_id=“1” column_key=“stat1” column_id=“angler_id” column_id_value=“[set_post_id]” sort=“1”]
“`
4. **ACF and Dynamic Tags**: If you opt for ACF, you could import the data using the ACF CSV import feature and then use Elementor’s dynamic tags to display the stats. ACF’s import feature can handle bulk updates efficiently, which could work well for your periodic updates.
5. **Bulk Update for ACF**: You can automate ACF field updates using a custom script that reads from your CSV or database and updates the relevant fields for each angler post.
Using the single table approach with PHP to dynamically assign post IDs should reduce the database load and CSS reliance, providing a more scalable solution for your needs.
You made an unnecessary mess. Just save the data into the postmeta table and create a template for your CPT