I am trying to solve the following scenario in wp-admin:

When an agent logins in he should only be able to see the actors that have a relation with him. (on page: wp-admin/edit.php?post_type=actor) In the post from an actor (post_type actor) I have a relationship field (named: your_agent) which shows all the agents (post_type talent-agents).

The ID of the agents post is for example 1. The agent should only see the actors that selected his post. (post with ID 1)

When the ID of the selected post in the relationship is equal to the one from the agents post. The talent agent that owns that post (current logged in agent), should see that actor.

This is the code i have so far:

function current_user_related_posts($query)
    if( is_user_logged_in() ) { // check if there is a logged in user 
        $user = wp_get_current_user(); // getting & setting the current user 
        $roles = ( array ) $user->roles; // obtaining the role 
        return $roles; // return the role for the current user 
    } else {
        return array(); // if there is no logged in user return empty array  
    $args = array(
      'post_type' => 'actor',
      'post_status' => 'publish',
      'meta_query' => array(
        'relation' => 'AND',  
          'key' => 'talent-agents_id',
          'value' => $post_id,
          'compare' => '='
          'key' => 'your_agent',
          'value' => $post_id,
          'compare' => '='
      'fields' => 'id, your_agent'
    $query = new WP_Query( $args );

    if( current_user_can('install-plugins') ) {
    return $query; 

I used Advanced Custom Fields PRO to create the fields.

This is how the relationship field on the actor post looks like. enter image description here

  • I don't (yet) really understand the question, but there are issues in your code that need fixing - 1) WordPress (core) doesn't have a function named "wp_get_current_user_role" or "post_id"; 2) That 'relation' => 'AND', should belong in the meta_query array; 3) You used invalid values with the fields argument; 4) What is that $post_author->$current_user_id; doing there? 🤔 and 5) The WP_Query class doesn't have a method named "get_related_posts".
    – Sally CJ
    Commented May 19, 2021 at 11:43
  • Thank you for your response. I updated the question. Thank you for your feedback :) I am currently updating my code. So if I understand correctly, it is not possible to use variables like $post_id als value in a query? Commented May 19, 2021 at 12:50
  • @SallyCJ I update my code on the basis of the feedback you provided. I did not know what I exactly had to fill in by the values, cause I am not very familiar with the allowed values for wordpress queries. Commented May 19, 2021 at 13:03
  • You can use $post_id in the query, but that variable is not actually defined in your function. And, 1) "see the actors that have a relation with him" - see on what page; is it the Edit page for the "actors" (at wp-admin/edit.php?post_type=actor)? 2) How do you call the current_user_related_posts() function - in what template/file/hook? 3) How did you add the custom fields (using a plugin?) and what are the format of the field's values, e.g. talent-agents_id - is it a single ID (number) or a list like 1, 2, 3? How about the other custom field - what's the format?
    – Sally CJ
    Commented May 19, 2021 at 13:25
  • @SallyCJ I use Advanced Custom Fields PRO and I would indeed like to the posts to show on wp-admin/edit.php?post_type=actor. I am not calling the current_user_related_posts() function somewhere at the moment. talent-agents_id is a single id number. I was trying to refer to the id of a talent agent post wp-admin/edit.php?post_type=talent-agents all the talent agents are on that page. That are the posts that are shown by the relationship field in the actor post. I will add a screenshot of how the relationship field looks like. Commented May 19, 2021 at 13:48

1 Answer 1


Eventually I solved the problem. I displayed the actors in dynamic views element (from dynamic.ooo) on a frontend page.

I put the filter criteria like so: filter by these conditions field = relatie_test (user based relationship field) operator = LIKE value = user(ID)

That did the trick for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.