How to select nodes of a certain type, created by a certain user and which have reference field to a node equal a certain nid?


The EntityFieldQuery class was made for just that:

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', $type)
  ->properyCondition('uid', $user_id)
  ->fieldCondition('field_name_of_field', 'target_id', $nid);

$results = $query->execute();

if (!empty($results['node'])) {
  $nodes = node_load_multiple(array_keys($results['node']));

Note that target_id is the name of the column for an Entity Reference field; if you're using the References module it will be nid instead.

This code assumes you're using the entityreference module using field name "field_reference":

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node');
$query->propertyCondition('type', $type);
$query->propertyCondition('uid', $userid);
$query->fieldCondition('field_reference', 'target_id', $referenceid);
$result = $query->execute();

$nodes = array();
if (!empty($result['node'])) {
  $nodes = node_load_multiple(array_keys($result['node']));
Tags: Nodes

Similar questions

how to change node edit path from node/[nid]/edit to node/[nid]/update?
I have Pathauto which changes node/[nid] to having a path of [title]; I then use subpathauto to change node/[nid]/edit to [title]/edit. But now how can I change the edit fragment? I want to change all instances of edit to update. so the URLs would be [title]/update instead of [title]/edit. I cannot use arguments or wildcards in Pathauto, so I canno...
Get future node id (nid) of nodes being programmatically created (future node)
I want to create multiple nodes using one form, but I'd like to have connection between them stored in Entity Reference, so I need to know their nids. But they aren't created yet, so they hasn't nids. Question: Is there a way to predict future nodes nids and preserve them for a while or do something similar to deal with task above?
How to pass a nid to panel, and load a node with the given nid as context?
The panel has a custom path, Like this: So the arguments to the panel are: How can I load a node with some variable nid (theNID in this case), and have it available as a context in the panel, and then, make all of it's fields accessible directly in that panel? If I create a view and load th...
How to update some field of all article nodes programatically and set it equal to its title?
I have a field name 'field_new' in a content type 'article'. I want to insert the value of node title on this field (all the nodes) programatically. What I have done to achieve, I am using hook_node_update for this, but it is not updating the field values. It would be great if there are another alternatives to do this.
Having a content type with a CCK file field, how to set up a view to only show nodes of that content type which do not have a file uploaded?
I have explained my problem here I posted this earlier in text but no one could get the idea about my problem so I made the video. It's only 4 minutes long. I'd appreciate if anyone can help. It's the last hurdle for me to finish this university project. EDIT: Someone has done something similar to what I need at drupal. org/n...
Select users that do have certain roles and do not have others
I need some help with my SQL in that I want to get all users with a certain role that do have another role. My SQL is really bad so any help would be appreciated.

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.