Support » Plugins » $wpdb runs only once in first loop cycle

  • Hi everyone

    I’ve got strange problem. I think that code looks fine but when I am trying to get data from custom table in WordPress database it takes only first record.

    In file content-single.php I’ve got this line of code where I am calling get_product_data() function;

    <div class="entry-content">
         <?php the_content(); ?>
         <?php get_product_data(); ?>

    Then in functions.php I’ve got two functions:

    function get_product_data()
     	global $wpdb;
    	$meta_table = $wpdb->prefix.'postmeta';
    	$postid = get_the_ID();
    	$eans = $wpdb->get_results("SELECT meta_value FROM $meta_table WHERE post_id = $postid AND meta_key = '_bs_prod_sym'");
    	$eans = explode(';', $eans[0]->meta_value);
    	foreach($eans as $ean)
    function get_from_database($ean)
    	global $wpdb;
    	$product_table = $wpdb->prefix.'_features';
    	echo '<table><tr><th>EAN</th><th>Cena</th>';
    	$features = $wpdb->get_results("SELECT feature_name FROM $product_table WHERE product_id = '$ean'");
    	foreach($features as $feature)
    		echo '<th>';
    			echo $feature->feature_name;
    		echo '</th>';
    	echo '</tr><tr><td>'.$ean.'</td><td></td>';
    	//$features = $wpdb->get_results("SELECT feature_name FROM $product_table WHERE product_id = '$ean'");
    	$values = $wpdb->get_results("SELECT feature_value FROM $product_table WHERE product_id = '$ean'");
    	foreach($values as $value)
    		echo '<td>';
    			echo $value->feature_value;
    		echo '</td>';
    	echo '</tr></table>';
    	return true;

    In array $eans I’ve got 4 values, names of tables in database are good and these values are inside. Maybe some can help me and know why I can get from database values from first cycle in the foreach loop;

    Best regards

  • The topic ‘$wpdb runs only once in first loop cycle’ is closed to new replies.