Ok i managed to find a solution, but I still don’t understand why it happened. If i change my code to:
add_action( "loop_start", "start" );
function start( $post )
{
if(is_singular())
{
global $wpdb;
$USER_ACHIEVEMENTS_DB = $wpdb->prefix.PG_USER_ACHIEVEMENTS;
$userAchievement = $wpdb->get_row("SELECT * FROM $USER_ACHIEVEMENTS_DB WHERE achievement = 'Explorer' AND user_id = 1");
$currentScore =(int)$userAchievement->score;
//Print just before
echo "before $currentScore";
$currentScore += 1; //increment
//Call the update
$wpdb->update( "$USER_ACHIEVEMENTS_DB", array( 'stage' => 1, 'level' => $currentScore, 'score' => $currentScore ),
array( 'user_id' => 1, 'achievement' => "Explorer" ),
array( '%d', "%s", '%d', ),
array( '%d', '%s' ));
echo "after $currentScore"; //This is correct. but in the DB its always double what I increment it with.
die("FIN");
}
else
{
echo "This never gets called??"; //Why if i add the singular check does this never get called?
}
}
Then it works ok, but the else never gets called. So how can this function be getting called twice?