Support

Account

Home Forums Backend Issues (wp-admin) Display post when its id is equal to the post in a relationship Reply To: Display post when its id is equal to the post in a relationship

  • Since i can’t update the question, I will clearify it more this way.

    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. 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.

    I updated the code to this:

    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',  
            array(
              'key' => 'talent-agents_id',
              'value' => $post_id,
              'compare' => '='
            ),
            array(
              'key' => 'your_agent',
              'value' => $post_id,
              'compare' => '='
            )
          ),
          'fields' => 'id, your_agent'
        );
        $query = new WP_Query( $args );
    
        if( current_user_can('install-plugins') ) {
            $query->the_post('actor');
        }
        return $query; 
    }