Warning message

Documentation is currently being migrated into the new system. Some pages might be temporarily missing, and some guides might appear empty. Thank you for your patience while we are improving Drupal.org documentation.


Create a Rule to Evaluate If a Logged-in User Has Created Content of Type X

Last updated on
November 1, 2016 - 16:31

I recently encountered a situation where I wanted to redirect a user to create a custom content type on login if that content doesn't already exist for the user. Profile2 and Account both provide token support for these fields through Rules, so depending on your needs and how your content is related to the user, you might be able to use these. For my custom type, I was unable to use tokens, so I had write some custom PHP to test if content exists of a certain content type for a given user. Here's what worked for me:

1. Create a rule (you must download and install the contrib Rules module to do this). I called mine "Redirect User at Login."
2. Select React on Event > User > User has logged in.
3. From Conditions, select the condition to add: PHP > Execute custom PHP code.
4. On the Add a new condition page, in the PHP code, Value field, enter (without PHP brackets):

global $user;
$result = db_query("SELECT COUNT(nid) FROM node JOIN users ON node.uid = users.uid WHERE node.status = :status AND users.uid = :uid AND node.type = :type", array(":status"=> 1, ":uid" => $user->uid, ":type" => 'configuration_1'))->fetchField();
if ($result >= 1){ 
return FALSE;
return TRUE;

In this code, the SQL query checks the database for a user who has created one or more nodes of the type configuration_1. If one or more nodes exist for this user, the condition evaluates to false, and the user will not be redirected to the node/add page. If no nodes exist for the user, the code returns true, and the user will be redirected. Node status "1" means the node is published. Be sure to declare the global $user variable before the query.

5. When you have saved the code, go to Actions, Add action. Select System > Page Redirect. Now add the URL of the page you want to redirect the user to. In my case, it's node/add/configuration-1.

Save your rule and set the weight so that it is triggered in the proper order in relation to any other rules you may have. Make sure the rule is set to Active. Test the rule. You're good to go.