Hi,
thanks for your question.
The connection between table ID and post ID is stored in the option tablepress_tables
in the wp_options
table in the database, which is a JSONified array.
Regards,
Tobias
Nice question and answer. I want to share how it worked for me 🙂
<?php
// get ACF saved data.
$post_obj = get_field( 'my_table' );
// get the post-table pair json data
$table_json = get_option( 'tablepress_tables' );
// json decode to array
$json_dec = json_decode( $table_json, true );
// get the pair data
$post_table = $json_dec['table_post'];
// flip the key/value of the array
$flip = array_flip( $post_table );
// you get the table id from postID by $flip[$post_obj->ID]
$shortcode = '[table id=' . $flip[$post_obj->ID] . ' /]';
// paste this in your template file :)
echo do_shortcode( $shortcode );
Hi,
thanks for sharing this! Very nice solution!
Best wishes,
Tobias
P.S.: In case you haven’t, please rate TablePress here in the plugin directory. Thanks!
Thanks for the reply Tobias!
I rated, thank you for this plugin.
And about this topic, I felt it’s easier to let clients to put the ID of the table.
You can put not only numbers but also texts like ‘Bangkok_tour_price_1’.
Hi,
thanks, I really appreciate that! 🙂
Best wishes,
Tobias
@shinichin, thanks for the snippet!
@tobiasbg, awesome plugin! Just rated it 5 starts.
Here’s an update of the version that I’ve decided to use.
I typically try to avoid do_shortcode
for reasons explained here: http://kovshenin.com/2013/dont-do_shortcode/
So I rewrote @shinichin’s function to use @tobiasbg’s recommended template tag.
The code below includes my ACF post object field but this can be used without as-well.
Hope it helps!
With ACF Post Object:
<?php // Init Post Object
$postobject = get_sub_field('table');
$post = $postobject;
setup_postdata( $post );
// tablepress plugin query / args
// get ACF saved data.
$post_obj = get_sub_field( 'table' );
// get the post-table pair json data
$table_json = get_option( 'tablepress_tables' );
// json decode to array
$json_dec = json_decode( $table_json, true );
// get the pair data
$post_table = $json_dec['table_post'];
// flip the key/value of the array
$flip = array_flip( $post_table );
// table args
$args = array(
'id' => $flip[$post_obj->ID],
'use_datatables' => true,
'print_name' => false
);
tablepress_print_table( $args); ?>
<?php // Reset data from Post Object
wp_reset_postdata(); ?>
Without ACF Post Object:
<?php
// tablepress plugin query / args
// get ACF saved data.
$post_obj = get_sub_field( 'table' );
// get the post-table pair json data
$table_json = get_option( 'tablepress_tables' );
// json decode to array
$json_dec = json_decode( $table_json, true );
// get the pair data
$post_table = $json_dec['table_post'];
// flip the key/value of the array
$flip = array_flip( $post_table );
// table args
$args = array(
'id' => $flip[$post_obj->ID],
'use_datatables' => true,
'print_name' => false
);
tablepress_print_table( $args); ?>
Hi,
very nice! Yes, using the template tag functions instead of do_shortcode()
is much better and faster here 🙂
Best wishes,
Tobias