Support

Account

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

Solved

Display post when its id is equal to the post in a relationship

  • I am trying to show the posts from a relationship on a backend page (wp-admin).
    But I am stuck on how to show the posts.

    The relationship has a field named your_agent and has a list of all the talent-agent posts.
    I am trying to compair the id of the talent-agent post (talent-agents_id) to the post id of the post that is stored in the relationship. When both are equal the actor post that has a relation with the talent-agents post(talent-agents_id), should show up on the page.

    roles:
    talent-agent & actor (an actor selects his talent agent in a relationship field named your_agent)

    posts:
    talent-agents & actor (the talent-agents post is listed in the relationship with field name: your_agent)

    This is the code i have so far:

    function current_user_related_posts($query)
    {
    	$current_user_id = get_current_user_id();
    	$current_user = wp_get_current_user_role();
    	$post_author = post_id();
    	
     	$args = array(
     	  'post_type' => 'actor',
     	  'post_status' => 'publish',
     	  'relation' => 'AND',
     	  'meta_query' => array(
     		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') ) {
    		$post_author->$current_user_id;
    		$query->get_related_posts($post_author);
    	}
    	return $query;
    }

    But nothing is getting displayed at the moment. Could anyone tell me if my code makes any sense? Cause I am completely lost.

  • 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; 
    }
  • 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.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.