Hey there, Awesome Customers!

Just a heads up: We'll be taking a breather to celebrate International Workers' Day (May 1st and 2nd - Wednesday and Thursday) and Orthodox Easter from Good Friday (May 3rd) through Easter Monday (May 6th). So, from May 1st to May 6th, our team will be off enjoying some well-deserved downtime.

During this time, our customer support will be running on a smaller crew, but don't worry! We'll still be around to help with any urgent matters, though it might take us a bit longer than usual to get back to you.

We'll be back in action at full throttle on May 7th (Tuesday), ready to tackle your questions and requests with gusto!

In the meantime, you can explore our documentation for Amelia and wpDataTables. You'll find loads of helpful resources, including articles and handy video tutorials on YouTube (Amelia's YouTube Channel and wpDataTables' YouTube Channel). These gems might just have the answers you're looking for while we're kicking back.

Thanks a bunch for your understanding and support!

Catch you on the flip side!

Warm regards,

TMS

Okay
  Public Ticket #2288773
using %CURRENT_POST_ID% to get the ORIGINAL post_id of a translated post
Closed

Comments

  •  1
    Florian Huber started the conversation

    Hello!

    We are running a multilingual site. One use of wpDataTables there is technical data on individual product pages, which get translated into about ten languages. 

    We have put the post_id of the original posts (=products) into the product table in the database as an ID field, so we can select the data for every product based on the ID of its post (you open product page of product XY, wpdt takes the %CURRENT_POST_ID% and uses it in a SELECT statement to get the data for product XY "WHERE product_ID IS %CURRENT_POST_ID%") 

    This already works well, but ONLY in the original language, because the post_IDs of the translations are different.


    Do you know of any way to get the ID of the original post, to pass it to wpdt as a variable instead?

    something like:

    [wpdatatable id=93 var3=$original_post_id]


    I know this is not exactly a wpdatatables issue/functionality, but maybe you have come across this before and know of a solution...


    Thank you 

    Florian


  •  2,498
    Aleksandar replied

    Hello again Florian.

    Using the built-in features of the plugin will not be able to do this, however there is a workaround using this hook:

    wpdatatables_before_get_table_metadata( $tableId )

    Although, there is a bug on our end, and I'll have to ask you to bear with me to get it working properly.

    The action is called correctly when modifying a table in the backend, but if you have the same table rendered from the front-end the action is not called correctly.

    If you change a global variable within this action - works in the backend, in the front-end the default value is used:

    function testSetVar1toMNR($MyTable){
      global $wdtVar1;
      //get Column from Plugin
      $lMNR = do_shortcode('[ihc-user field="CUSTOM_FIELD_column"]');
      //Found?
      if ($lMNR>0) {
       $wdtVar1 = $lMNR; //Set VAR1 custom parameter to column
      }
      else
      {
          $wdtVar1 = "9999"; //Default to 9999 --> needed for Admin Context
         }
           }
    add_action('wpdatatables_before_get_table_metadata', 'testSetVar1toMNR');
    

    Unfortunately at the moment I can offer only this Hot-Fix with non desired-side-effects:

    public function fillFromData($tableData, $columnData) {
            if (empty($tableData->table_type)) {
                return;
            }
            global $wdtVar1, $wdtVar2, $wdtVar3;
        //test: Change 31.07.2019 to update placeholders in production mode
      //needs default value for admin-context, otherwise breaks table creation
      do_action('wpdatatables_before_get_table_metadata');
            // Set placeholders
            $wdtVar1 = $wdtVar1 === '' ? $tableData->var1 : $wdtVar1;
    

    ….SNIP

    Here:  class.wpdatatable.php

    %CURRENT_POST_ID% returns the ID of the current post, but with this you should be able to get the Post ID you need, and use it as %VAR1%.

    I hope this helps.


    Kind Regards, 

    Aleksandar Vuković
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables

  •  1
    Florian Huber replied

    Hello Aleksandar, thank you for the detailed reply.

    I have found a different way. I found a combination of functions to get the correct ID and wrote a plugin to wrap the [wpdatatable] shortcode into a custom shortcode in order to pass the ID to wpdatatables automatically.

    I first started using the function icl_object_id() :

    $orig_ppost = icl_object_id(get_the_ID(), 'product', false, 'de');

    which did work, but I found out that this function is deprecated, so I switched to this new approach:

    $orig_ppost = apply_filters( 'wpml_object_id', get_the_ID(), 'product', false, 'de');

    which returns the same value. get_the_ID() gets the ID of the current post in whatever language the site is opened, then the function returns the value of the "basic" post ID in the original language (in our case, German="de").

    The tricky bit was to find a way to pass the variable to the [wpdatatable] shortcode. I haven´t found a way to do that in the actual html post.

    So I built a  kind of "shortcode wrapper" which creates a new shortcode ( [callwpdatatable] ) I can use in the actual post/html page, which wraps a function, which in turn calls the [wpdatatable] shortcode and passes it this variable and a few others, like so:

    echo do_shortcode( '[wpdatatable id=' . $wpdt_id . ' var1=' . $clean_lang_code . ' var2="' . $orig_ppost . '" ]' );

    $wpdt_id is the wpdatatable_ID of the correct table (the same one for all the products)

    $clean_lang_code is the ID of the current frontend language

    $orig_ppost is the ID of the current post´s original German version.

    I chose not to temper around with the functions.php of our child theme, so I actually wrote a little plugin for all this.

    So now I have a custom shortcode I can use on all my product pages without having to set any values manually, as the plugin finds out the ID of the original German post (which I use in my mySQL product table to identify the correct product), and passes it and the current language ID to wpDatatables. I just plug in the identical shortcode into my posts and automatically get the correct product table in the correct language. 

    Yay! :o)


  •  2,498
    Aleksandar replied

    Hello again Florian

    Wow, that's simply awesome. Great work!

    I'm glad to hear you found a way to do it, and happy that you'll have the use of wpDataTables.

    I will forward this ticket to our development team, and maybe they'll get an idea of how it can be implemented in the plugin for the future.

    Kind Regards, 

    Aleksandar Vuković
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables

  •   Florian Huber replied privately
  •  1
    Florian Huber replied

    Here´s the SQL query I used, to get the list seen at https://kittec.eu/de/produkte/cb-60/ 

    It comes with a bug report for you, I think. Again, I found a workaround for this on my side, so you don´t have to do anything, but you might want to know about it.

    You can see that I used a lot of CONCAT() to add units (like "[mm]") to fields. But I had to put a field at the very end of the query which doesn´t NEED a unit, because CONCAT() raises an error when put at the very end. 

    The query below works without error, but when I remove the last SELECT in this query (everything from the last UNION), I get this error message:

    Error

    cannot calculate position of (wp_3o9yizi0oq_wpdatatable_14.`trm032`,, ' [',wp_3o9yizi0oq_wpdatatable_18.`term`,']') within UNION SELECT Concat(wp_3o9yizi0oq_wpdatatable_14.`trm032`,, ' [',wp_3o9yizi0oq_wpdatatable_18.`term`,']') AS 'Gewicht',


    So with the un-concatenated SELECT at the end of the query, the exactly same 

    SELECT Concat(wp_3o9yizi0oq_wpdatatable_14.`trm032`,, ' [',wp_3o9yizi0oq_wpdatatable_18.`term`,']') AS 'Gewicht', 

    (which is then the last-but-one) works without Error! If it´s the last element of the query, it raises an error!


    Here´s the entire query:

    SELECT wp_3o9yizi0oq_wpdatatable_14.`trm040` AS 'Beschriftung',
           MID(wp_3o9yizi0oq_wpdatatable_36.`name%VAR1%`,INSTR(wp_3o9yizi0oq_wpdatatable_36.`name%VAR1%`,'||')+2) AS 'Wert'
    FROM wp_3o9yizi0oq_wpdatatable_14, wp_3o9yizi0oq_wpdatatable_36
    WHERE 1=1 
       AND wp_3o9yizi0oq_wpdatatable_14.`langID` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_36.`idde` = '%VAR2%'
          UNION
    SELECT wp_3o9yizi0oq_wpdatatable_14.`trm005` AS 'Bauart',
           wp_3o9yizi0oq_wpdatatable_15.`term`  
    FROM wp_3o9yizi0oq_wpdatatable_14, wp_3o9yizi0oq_wpdatatable_36 INNER JOIN wp_3o9yizi0oq_wpdatatable_15 ON wp_3o9yizi0oq_wpdatatable_36.`BauartID` = wp_3o9yizi0oq_wpdatatable_15.`termID`
    WHERE 1=1 
       AND wp_3o9yizi0oq_wpdatatable_14.`langID` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_36.`idde` = '%VAR2%'
          AND wp_3o9yizi0oq_wpdatatable_15.`langID` = '%VAR1%'
    UNION
    SELECT wp_3o9yizi0oq_wpdatatable_14.`trm006` AS 'Energie',
           wp_3o9yizi0oq_wpdatatable_16.`term`  
    FROM wp_3o9yizi0oq_wpdatatable_14, wp_3o9yizi0oq_wpdatatable_36 INNER JOIN wp_3o9yizi0oq_wpdatatable_16 ON wp_3o9yizi0oq_wpdatatable_36.`EnergieID` = wp_3o9yizi0oq_wpdatatable_16.`termID`
    WHERE 1=1 
       AND wp_3o9yizi0oq_wpdatatable_14.`langID` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_36.`idde` = '%VAR2%'
          AND wp_3o9yizi0oq_wpdatatable_16.`langID` = '%VAR1%'
    UNION
    SELECT Concat(wp_3o9yizi0oq_wpdatatable_14.`trm041`, ' [',wp_3o9yizi0oq_wpdatatable_18.`term`,']') AS 'MaxTemp',
           wp_3o9yizi0oq_wpdatatable_36.`temperaturmax`
           
    FROM wp_3o9yizi0oq_wpdatatable_14, wp_3o9yizi0oq_wpdatatable_36, wp_3o9yizi0oq_wpdatatable_18 
    WHERE 1=1 
       AND wp_3o9yizi0oq_wpdatatable_14.`langID` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_36.`idde` = '%VAR2%'
          AND wp_3o9yizi0oq_wpdatatable_18.`langid` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_18.`termid` = '3'
    UNION
    SELECT Concat(wp_3o9yizi0oq_wpdatatable_14.`trm037`, ' [',wp_3o9yizi0oq_wpdatatable_18.`term`,']') AS 'Leistung',
           wp_3o9yizi0oq_wpdatatable_36.`leistungkw`
           
    FROM wp_3o9yizi0oq_wpdatatable_14, wp_3o9yizi0oq_wpdatatable_36, wp_3o9yizi0oq_wpdatatable_18 
    WHERE 1=1 
       AND wp_3o9yizi0oq_wpdatatable_14.`langID` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_36.`idde` = '%VAR2%'
          AND wp_3o9yizi0oq_wpdatatable_18.`langid` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_18.`termid` = '6'

     UNION
    SELECT Concat(wp_3o9yizi0oq_wpdatatable_14.`trm038`, ' [',wp_3o9yizi0oq_wpdatatable_18.`term`,']') AS 'Spannung',
           wp_3o9yizi0oq_wpdatatable_36.`spannungv`
           
    FROM wp_3o9yizi0oq_wpdatatable_14, wp_3o9yizi0oq_wpdatatable_36, wp_3o9yizi0oq_wpdatatable_18 
    WHERE 1=1 
        AND wp_3o9yizi0oq_wpdatatable_14.`langID` = '%VAR1%'
        AND wp_3o9yizi0oq_wpdatatable_36.`idde` = '%VAR2%'
          AND wp_3o9yizi0oq_wpdatatable_18.`langid` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_18.`termid` = '5'
    UNION
    SELECT Concat(wp_3o9yizi0oq_wpdatatable_14.`trm008`, ' [',wp_3o9yizi0oq_wpdatatable_18.`term`,']') AS 'Anschluss',
           wp_3o9yizi0oq_wpdatatable_36.`stroma` 
           
    FROM wp_3o9yizi0oq_wpdatatable_14, wp_3o9yizi0oq_wpdatatable_36, wp_3o9yizi0oq_wpdatatable_18 
    WHERE 1=1 
       AND wp_3o9yizi0oq_wpdatatable_14.`langID` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_36.`idde` = '%VAR2%'
          AND wp_3o9yizi0oq_wpdatatable_18.`langid` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_18.`termid` = '4'

    UNION
    SELECT Concat(wp_3o9yizi0oq_wpdatatable_14.`trm044`, ' [',wp_3o9yizi0oq_wpdatatable_18.`term`,']') AS 'Volumen',
           wp_3o9yizi0oq_wpdatatable_36.`volumen`
           
    FROM wp_3o9yizi0oq_wpdatatable_14, wp_3o9yizi0oq_wpdatatable_36, wp_3o9yizi0oq_wpdatatable_18 
    WHERE 1=1 
       AND wp_3o9yizi0oq_wpdatatable_14.`langID` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_36.`idde` = '%VAR2%'      
          AND wp_3o9yizi0oq_wpdatatable_18.`langid` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_18.`termid` = '2'

    UNION

    SELECT Concat(wp_3o9yizi0oq_wpdatatable_14.`trm024`, ' [',wp_3o9yizi0oq_wpdatatable_18.`term`,']') AS 'BRDurchmesser',
           wp_3o9yizi0oq_wpdatatable_36.`durchmbr`
           
    FROM wp_3o9yizi0oq_wpdatatable_14, wp_3o9yizi0oq_wpdatatable_36, wp_3o9yizi0oq_wpdatatable_18 
    WHERE 1=1 
       AND wp_3o9yizi0oq_wpdatatable_14.`langID` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_36.`idde` = '%VAR2%'            
          AND wp_3o9yizi0oq_wpdatatable_18.`langid` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_18.`termid` = '1'

    UNION
    SELECT Concat(wp_3o9yizi0oq_wpdatatable_14.`trm023`, ' [',wp_3o9yizi0oq_wpdatatable_18.`term`,']') AS 'BRBreite',
           wp_3o9yizi0oq_wpdatatable_36.`breitebr`
           
    FROM wp_3o9yizi0oq_wpdatatable_14, wp_3o9yizi0oq_wpdatatable_36, wp_3o9yizi0oq_wpdatatable_18 
    WHERE 1=1 
       AND wp_3o9yizi0oq_wpdatatable_14.`langID` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_36.`idde` = '%VAR2%'            
          AND wp_3o9yizi0oq_wpdatatable_18.`langid` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_18.`termid` = '1'

    UNION
    SELECT Concat(wp_3o9yizi0oq_wpdatatable_14.`trm025`, ' [',wp_3o9yizi0oq_wpdatatable_18.`term`,']') AS 'BRBoehe',
           wp_3o9yizi0oq_wpdatatable_36.`hhebr`
           
    FROM wp_3o9yizi0oq_wpdatatable_14, wp_3o9yizi0oq_wpdatatable_36, wp_3o9yizi0oq_wpdatatable_18 
    WHERE 1=1 
       AND wp_3o9yizi0oq_wpdatatable_14.`langID` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_36.`idde` = '%VAR2%'                  
          AND wp_3o9yizi0oq_wpdatatable_18.`langid` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_18.`termid` = '1'
    UNION
    SELECT Concat(wp_3o9yizi0oq_wpdatatable_14.`trm026`, ' [',wp_3o9yizi0oq_wpdatatable_18.`term`,']') AS 'BRTiefe',
           wp_3o9yizi0oq_wpdatatable_36.`tiefebr`
           
    FROM wp_3o9yizi0oq_wpdatatable_14, wp_3o9yizi0oq_wpdatatable_36 , wp_3o9yizi0oq_wpdatatable_18
    WHERE 1=1 
       AND wp_3o9yizi0oq_wpdatatable_14.`langID` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_36.`idde` = '%VAR2%'                        
          AND wp_3o9yizi0oq_wpdatatable_18.`langid` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_18.`termid` = '1'
    UNION
    SELECT Concat(wp_3o9yizi0oq_wpdatatable_14.`trm010`, ' [',wp_3o9yizi0oq_wpdatatable_18.`term`,']') AS 'Aussenbreite',
           wp_3o9yizi0oq_wpdatatable_36.`Aussenbreite`
           
    FROM wp_3o9yizi0oq_wpdatatable_14, wp_3o9yizi0oq_wpdatatable_36 , wp_3o9yizi0oq_wpdatatable_18
    WHERE 1=1 
       AND wp_3o9yizi0oq_wpdatatable_14.`langID` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_36.`idde` = '%VAR2%'
          AND wp_3o9yizi0oq_wpdatatable_18.`langid` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_18.`termid` = '1'
    UNION
    SELECT Concat(wp_3o9yizi0oq_wpdatatable_14.`trm016`, ' [',wp_3o9yizi0oq_wpdatatable_18.`term`,']') AS 'Aussentiefe',
           wp_3o9yizi0oq_wpdatatable_36.`Aussentiefe`
           
    FROM wp_3o9yizi0oq_wpdatatable_14, wp_3o9yizi0oq_wpdatatable_36, wp_3o9yizi0oq_wpdatatable_18 
    WHERE 1=1 
       AND wp_3o9yizi0oq_wpdatatable_14.`langID` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_36.`idde` = '%VAR2%'                  
          AND wp_3o9yizi0oq_wpdatatable_18.`langid` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_18.`termid` = '1'
    UNION
    SELECT Concat(wp_3o9yizi0oq_wpdatatable_14.`trm014`, ' [',wp_3o9yizi0oq_wpdatatable_18.`term`,']') AS 'Aussenhöhe',
           wp_3o9yizi0oq_wpdatatable_36.`aussenhheinkluntergestell`
           
    FROM wp_3o9yizi0oq_wpdatatable_14, wp_3o9yizi0oq_wpdatatable_36, wp_3o9yizi0oq_wpdatatable_18 
    WHERE 1=1 
       AND wp_3o9yizi0oq_wpdatatable_14.`langID` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_36.`idde` = '%VAR2%'                  
          AND wp_3o9yizi0oq_wpdatatable_18.`langid` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_18.`termid` = '1'
    UNION
    SELECT Concat(wp_3o9yizi0oq_wpdatatable_14.`trm015`, ' [',wp_3o9yizi0oq_wpdatatable_18.`term`,']') AS 'AussenhöheUGgedreht',
           wp_3o9yizi0oq_wpdatatable_36.`aussenhheuntergestellgedreht` 
           
    FROM wp_3o9yizi0oq_wpdatatable_14, wp_3o9yizi0oq_wpdatatable_36, wp_3o9yizi0oq_wpdatatable_18 
    WHERE 1=1 
       AND wp_3o9yizi0oq_wpdatatable_14.`langID` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_36.`idde` = '%VAR2%'                        
          AND wp_3o9yizi0oq_wpdatatable_18.`langid` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_18.`termid` = '1'
    UNION
    SELECT Concat(wp_3o9yizi0oq_wpdatatable_14.`trm032`,, ' [',wp_3o9yizi0oq_wpdatatable_18.`term`,']') AS 'Gewicht',
           wp_3o9yizi0oq_wpdatatable_36.`gewichtkg`
           
    FROM wp_3o9yizi0oq_wpdatatable_14, wp_3o9yizi0oq_wpdatatable_36 , wp_3o9yizi0oq_wpdatatable_18
    WHERE 1=1 
       AND wp_3o9yizi0oq_wpdatatable_14.`langID` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_36.`idde` = '%VAR2%'
          AND wp_3o9yizi0oq_wpdatatable_18.`langid` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_18.`termid` = '7'

    UNION
    SELECT wp_3o9yizi0oq_wpdatatable_14.`trm007` AS 'Form',
           wp_3o9yizi0oq_wpdatatable_17.`term`  
    FROM wp_3o9yizi0oq_wpdatatable_14, wp_3o9yizi0oq_wpdatatable_36 INNER JOIN wp_3o9yizi0oq_wpdatatable_17 ON wp_3o9yizi0oq_wpdatatable_36.`FormID` = wp_3o9yizi0oq_wpdatatable_17.`termID`
    WHERE 1=1 
       AND wp_3o9yizi0oq_wpdatatable_14.`langID` = '%VAR1%'
          AND wp_3o9yizi0oq_wpdatatable_36.`idde` = '%VAR2%'
          AND wp_3o9yizi0oq_wpdatatable_17.`langID` = '%VAR1%'





  •  1
    Florian Huber replied

    And for good measure, here is the php code I put in a custom plugin to call the wpdatatable. We only need to paste [callwpdatatable] into any product post for which there is technical product data in our database, and get a display like this for the right product in the right language. Maybe this may give somebody else ideas how to do this:


    <?php

    // This code is used to use ONE wpDataTable to display technical product data of many different products in various translated languages.
    // This way we only have to change ONE wpDatatable (for instance its layout) and have the changes immediately reflected in all products.
    // The goal was to get ONE shortcode that can be pasted in EVERY product post without the need to set any variables, not even the proper ID of the wpDataTable itself.
    // in order to get this working, we need to find out the post ID of the ORIGINAL post (the German original on which all translations are based).
    // Because its unique to each product it can be used to get the product data from the mySQL table (the technical data is identical and unique, no matter in which language). 
    // Also we get the language ID in which the product is currently displayed (the language the user sets for the website), so we can get the proper descriptions for the technical data.
    // We also set the ID of the actual wpDataTable here, so we only need to change that in one place, should we ever want to switch to a different version. 
    // The function that does it all is wrapped in a shortcode called [callwpdatatables], which is all that needs to be put on the product page to get the technical data of THAT product in the current language.

    // Add Function to wrap wpDataTables with dynamic parameters:
    function call_wpdatatable($wp_table_version) {

    //We need to get/set the three variables we need to pass to wpDatatables in order to display 1. the correct wpDataTable 2. of the right product 3. in the right language:
    //1. which table to use? Set $wpdt_id to the right ID:
    // First we extract the (optional, to select different versions of the same table) custom "table version" parameter from the function´s parameter array...
    extract(shortcode_atts(array(
            'wp_table_version' => 'wp_table_version'
        ), $wp_table_version));
    // then we branch into different versions of the wpdatatables table, e.g. to show different kinds of tech data (Currently not used), 
    // and set the variable to the wpDataTable ID we want to use (93, currently):
    if ($wp_table_version==1) { 
    $wpdt_id = 93 ; // wpDataTable ID, hardcoded. If a swap to a different table version is needed, this is where to put the new ID.
    } elseif ($wp_table_version==2) {
    $wpdt_id = 93 ; //
    } else {
    $wpdt_id = 93 ; //
    }

    //After 1. comes 3., of course: What language is currently displayed?
    //Set $raw_lang_code to the language code of the current page/post/the language the user chooses on the frontend:
    $raw_lang_code = ICL_LANGUAGE_CODE;
    // then set $clean_lang_code to a cleaned version of that code, in particular:
    // translate the czech language Code "cz" code into "cs" for mySQL database use. For complicated reasons it needs to be cz for our tables, not cs...
    if ($raw_lang_code == "cs") { 
    $clean_lang_code = "cz";
    } else {
    $clean_lang_code = $raw_lang_code;
    }

    //After 3. comes 2., the core of this function: find the correct ID of the ORIGINAL German post, which we use in the mySQL product table to identify products
    //we found two ways of doing it:
    //First, the obsolete approach. It worked, but the icl_object_id() function is deprecated, apparently, and should no longer be used:
    //$orig_ppost = icl_object_id(get_the_ID(), 'product', false, 'de'); 

    // This is the new and better way, which does the same thing. It "get_the_ID()" of the current post in whatever language is displayed
    // and finds the corresponding ID of the origingal German (de) post on which the translation is based:
    $orig_ppost = apply_filters( 'wpml_object_id', get_the_ID(), 'product', false, 'de');

    // Now we call wpDataTables with parameters 1., 2. and 3.:
    echo do_shortcode( '[wpdatatable id=' . $wpdt_id . ' var1=' . $clean_lang_code . ' var2="' . $orig_ppost . ']' );
    //To sum it up, here the wpDataTable number $wpdt_id uses an SQL query of our mySQL product table to display the correct product, identified by $orig_ppost and another table with labels in all available languages to label the data in the correct language, selected via $clean_lang_code...
    }
    //function ends here

    // To literally wrap it all up, this adds a shortcode to call the above function and display the wpDataTables table without having to set any parameters:
    add_shortcode( 'callwpdatatable', 'call_wpdatatable' );

    // now you only need to drop the shortcode [callwpdatatable] into any product page and get the correct table!

    ?>


  •  2,498
    Aleksandar replied

    Thank you for the awesome feedback Florian.

    As for the UNION thing - we are aware that it is causing issues, but 90% of the time, it works when you simply add "UNION ALL" at the end of a query. However, I'm glad you found a workaround for that as well, and that you are overall satisfied with the outcome.

    Our developers looked into this, and they too are very happy about the fact that you managed to get it to work like this. One plea, though - would you be able to send us that little plugin you created? We would definitely want to take a look at it, and try it out ourselves!

    Best regards to you, sir!

    Kind Regards, 

    Aleksandar Vuković
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables

  •  1
    Florian Huber replied

    Hello Aleksandar, thank you and you´re welcome. 

    The php I posted below is basically all that´s in the plugin, I only edited out the plugin-description comments, but the actual code is all there, as is the complete SQL query one reply further down. Of course it´s critical that I have a unique ID field in my mySQL table which corresponds to the post ID of the original German post in wordpress for it all to work. I´m sure you´ll be able to build something like that. ;o)

    Hope this helps!

    Florian

  •  1
    Florian Huber replied

    I actually managed to smuggle an Error into that code which was not there before, an extra quotation mark:

    echo do_shortcode( '[wpdatatable id=' . $wpdt_id . ' var1=' . $clean_lang_code . ' var2="' . $orig_ppost . ']' ); is wrong... 

    So for good measure, here is the entire code of the plugin again. In the meantime I added a section to style the table that is called by the plugin (we use different wpDataTables that have to look differently, so this is very useful):


    <?php
    /**
     * Plugin Name: custom functions
     * Plugin URI: http://www.yourwebsite.com
     * Description: custom php functions, specifically for customising wpDataTables
     * Version: 1.0
     * Author: your name
     * Author URI: http://www.yourwebsite.com
     */

    // This code is used to use ONE wpDataTable to display technical product data of many different products in various translated languages.
    // This way we only have to change ONE wpDatatable (for instance its layout) and have it immediately reflected in all products.
    // The goal was to get ONE shortcode that can be pasted in EVERY product post without the need to set any variables, not even the proper ID of the wpDataTable itself.
    // in order to get this working, we need to find out the post ID of the ORIGINAL post (the German original on which all translations are based).
    // Because its unique to each product it can be used to get the product data from the mySQL table (the technical data is identical and unique, no matter in which language). 
    // Also we get the language ID in which the product is currently displayed (the language the user sets for the website), so we can get the proper descriptions for the technical data.
    // We also set the ID of the actual wpDataTable here, so we only need to change that in one place, should we ever want to switch to a different version. 
    // The function that does it all is wrapped in a shortcode called [callwpdatatables], which is all that needs to be put on the product page to get the technical data of THAT product in the current language.

    // Add Function to wrap wpDataTables with dynamic parameters:
    function call_wpdatatable($wp_table_version) {
    //Extract table version to use from parameter array. Optional to select different versions of the same table:
    extract(shortcode_atts(array(
            'wp_table_version' => 'wp_table_version'
        ), $wp_table_version));

    //now get the three variables we need to pass to wpDatatables to display 1. the correct wpDataTable 2. of the right product 3. in the right language:
    //1. which table to use? Set $wpdt_id to the right ID:
    // Here we branch into different versions of the wpdatatables table, e.g. to show different kinds of tech data. Currently not used, though
    if ($wp_table_version==1) {
    $wpdt_id = 93 ; // wpDataTable ID, hardcoded. If a swap to a different table version is needed, this is where to put the new ID.
    } elseif ($wp_table_version==2) {
    $wpdt_id = 93 ; //
    } else {
    $wpdt_id = 93 ; //
    }

    //After 1. comes 3., of course: What language is currently displayed?
    //Set $raw_lang_code to the language code of the current page/post/the language the user chooses on the frontend:
    $raw_lang_code = ICL_LANGUAGE_CODE;
    // then set $clean_lang_code to a cleaned version of that code, in particular:
    //translate the czech language Code "cz" code into "cs" for mySQL database use. For complicated reasons it needs to be cz for our tables, not cs...
    if ($raw_lang_code == "cs") { 
    $clean_lang_code = "cz";
    } else {
    $clean_lang_code = $raw_lang_code;
    }

    //After 3. comes 2., the core of this function: find the correct ID of the ORIGINAL German post, which we use in the mySQL product table to identify products
    //we found two ways of doing it:
    //First, the obsolete approach. It worked, but the icl_object_id() function is deprecated, apparently, and should no longer be used:
    //$orig_ppost = icl_object_id(get_the_ID(), 'product', false, 'de'); 

    // This is the new and better way, which does the same thing. It "get_the_ID()" of the current post in whatever language is displayed
    // and finds the corresponding ID of the origingal German (de) post on which the translation is based:
    $orig_ppost = apply_filters( 'wpml_object_id', get_the_ID(), 'product', false, 'de');

    //this is a section to CSS-style the table independently of the wpDataTable settings page:
    echo "<style>.wpdt-c table {margin-left: 1px !important;}</style>"; //for instance, this aligns the table itself to the left side of the content area it appears in...

    // Now we call wpDataTables with parameters 1., 2. and 3.:
    echo do_shortcode( '[wpdatatable id=' . $wpdt_id . ' var1=' . $clean_lang_code . ' var2=' . $orig_ppost . ']' );
    }

    // Add Shortcode to call wpDataTables with parameters
    add_shortcode( 'callwpdatatable', 'call_wpdatatable' );

    ?>


  •  2,498
    Aleksandar replied

    Awesome, Florian

    Thank you very much. I forwarded it to our development team, and I'm sure they'll have a use of it.

    If you have any further questions or issues, please feel free to open a new ticket, and we'll gladly help.

    Best regards.

    Kind Regards, 

    Aleksandar Vuković
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables