• I hope not to write silly things and be able to explain my problem. I wrote a plugin sketch where I access the database with some queries , you report the code that works :

    <?php
    /*
    Plugin Name: Carica File
    URI: Url del plugin
    Description: Carica File schede prodotti
    Author: Riccardo Version: 1.0
    Author URI: http://lksnc.it/
    License: GPL2
    */
    
    // plugin path
    $wp_plugin_dir = substr(plugin_dir_path(__FILE__), 0, -1);
    define('RR_DIR', $wp_plugin_dir);
    
    // plugin url
    $wp_plugin_url = substr(plugin_dir_url(__FILE__), 0, -1);
    define('RR_URL', $wp_plugin_url);
    
    function rr_theme_options_panel(){
      add_menu_page('Theme page title', 'Carica File', 'manage_options', 'theme-options', 'rr_wps_theme_func', 'dashicons-media-document');
    }
    add_action('admin_menu', 'rr_theme_options_panel');
    
    function rr_wps_theme_func(){
                    echo '<div class="wrap"><div id="icon-options-general" class="icon32"><br></div><h2>Theme</h2></div>';
    
    global $wpdb;
    $RR_ID='5888';
    echo $RR_ID.'<br>';
    
    			//LEGGO TABELLA PER RECUPERARE SKU >>>> SELECT  <code>meta_value</code> FROM  <code>wp_postmeta</code> WHERE  <code>post_id</code> =  '5888' AND  <code>meta_key</code> =  '_sku'
    			//META KEY (SKU) POTREBBE ESSERE ANCHE VUOTO NEL CASO MI FERMO E AVVISO DDK VIA MAIL
    			$RR_SKU=$wpdb->get_row("SELECT * FROM $wpdb->postmeta WHERE post_id=". (int) $RR_ID ." AND meta_key='_sku'", ARRAY_A);
    			$RR_SKU=$RR_SKU['meta_value'];
    			echo $RR_SKU.'<br>';
    
    			//LEGGO TABELLA PER RECUPERARE LINGUA >>>> SELECT <code>language_code</code>  FROM  <code>wp_icl_translations</code> WHERE  <code>element_id</code> =  '5888'
    			$RR_lang=$wpdb->get_row("SELECT * FROM wp_icl_translations WHERE element_id=". (int) $RR_ID ."", ARRAY_A);
    			$RR_lang=$RR_lang['language_code'];
    			echo $RR_lang.'<br>';
    
    			//COSTRUISCO STRINGA PER MODIFICARE POST_CONTENT IN TABELLA WP_POSTS
    			$RR_product_description='example NEW TEXT PRODUCT ON CREATION';
    
    			//ESEGUO QUERY UPDATE >>>>>> UPDATE  <code>wp_posts</code> SET  <code>post_content</code> =  'descrizione xxx prodotto' WHERE  <code>ID</code> =  '5888'
    			$wpdb->update(
    					'wp_posts',
    					array(
    						'post_content' => $RR_product_description,	// stringa
    					),
    					array( 'ID' => $RR_ID ),
    					array(
    						'%s',	// valore1
    					),
    					array( '%d' )
    			);
    
    			//creo cartelle per contenere schede >>>>>> archividdk/prodotti/sku/lang/msds & archividdk/prodotti/sku/lang/data
    
    $xmess='ID: '.$RR_ID.' Sku: '.$RR_SKU.' Lang: '.$RR_lang;
    mail('[email protected]','new job published',$xmess);
    echo $xmess;
    
    }
    ?>

    select to access the database and retrieve the data , the update changes the content as intended .

    The same query ( SELECT ) put in function.php rather not produce any RESULT , WHILE update keeps working .

    add_action( 'transition_post_status', function ( $new_status, $old_status, $post )
    {
    
        if( 'publish' == $new_status && 'publish' != $old_status && $post->post_type == 'product' ) {
    		//RECUPERO ID PRODOTTO
            $RR_ID=$post->ID;
    		global $wpdb;
    
    					//LEGGO TABELLA PER RECUPERARE SKU >>>> SELECT  <code>meta_value</code> FROM  <code>wp_postmeta</code> WHERE  <code>post_id</code> =  '5888' AND  <code>meta_key</code> =  '_sku'
    					//META KEY (SKU) POTREBBE ESSERE ANCHE VUOTO NEL CASO MI FERMO E AVVISO DDK VIA MAIL
    					$RR_SKU=$wpdb->get_row("SELECT * FROM $wpdb->postmeta WHERE post_id=". (int) $RR_ID ." AND meta_key='_sku'", ARRAY_A);
    					$RR_SKU=$RR_SKU['meta_value'];
    
    					//LEGGO TABELLA PER RECUPERARE LINGUA >>>> SELECT <code>language_code</code>  FROM  <code>wp_icl_translations</code> WHERE  <code>element_id</code> =  '5888'
    					$RR_lang=$wpdb->get_row("SELECT * FROM wp_icl_translations WHERE element_id=". (int) $RR_ID ."", ARRAY_A);
    					$RR_lang=$RR_lang['language_code'];
    
    					//COSTRUISCO STRINGA PER MODIFICARE POST_CONTENT IN TABELLA WP_POSTS
    					$RR_product_description='example NEW TEXT PRODUCT ON CREATION';
    
    					//ESEGUO QUERY UPDATE >>>>>> UPDATE  <code>wp_posts</code> SET  <code>post_content</code> =  'descrizione xxx prodotto' WHERE  <code>ID</code> =  '5888'
    					$wpdb->update(
    							'wp_posts',
    							array(
    								'post_content' => $RR_product_description,	// stringa
    							),
    							array( 'ID' => $RR_ID ),
    							array(
    								'%s',	// valore1
    							),
    							array( '%d' )
    					);
    
    					//creo cartelle per contenere schede >>>>>> archividdk/prodotti/sku/lang/msds & archividdk/prodotti/sku/lang/data
    
    		$xmess='ID: '.$RR_ID.' Sku: '.$RR_SKU.' Lang: '.$RR_lang;
    		mail('[email protected]','new job published',$xmess);
    
        }
    }, 10, 3 );

    Can someone explain to me where I’m wrong ? I’d be very grateful . Riccardo

The topic ‘Select problem add_action in function.php’ is closed to new replies.