I have two Rules (created with the Rules module) that are both launched when an article is flagged with the Flag Approved:
- Rule 1. Publish the flagged article with the action Publish content.
- Rule 2. Count the amount of published articles by the author of the flagged article. Display a message when this number is 5.
I'm using the following code in the condition of Rule 2:
return MYMODULE_count_articles($flagged_node->uid) == 5;
where my custom module contains the function
function MYMODULE_count_articles($uid) {
$count = db_query("SELECT COUNT(*) FROM node WHERE uid=$uid AND status=1 AND type='article'")->fetchField();
return $count;
}
The condition of Rule 2 is evaluated as FALSE
when the 5th article is flagged with Approved, but evaluates as TRUE
when a 6th article is flagged.
I suspect that this problem is caused because the database hasn't been updated yet when Rule 2 launches, because debugging the $count
variable at the 5th flagging returns 4
instead of 5
. Also, executing the condition's code afterwards yields the correct value 5
.
Is this a know problem/phenomenon or a bug in the Rules module? Or is there an easy way to fix it (apart from changing the condition to ... == 4
, which is really dirty)?
node_save
then that link could contain the answer: Flush the flagged node's cache before getting the count. Maybe in the same function or by doing a separate function call...