Support

Account

Home Forums ACF PRO Steve Jobs and Bill Gates – Relationship issue

Solving

Steve Jobs and Bill Gates – Relationship issue

  • Hello,
    I am working with ACF PRO with relationships.
    I have used the ACF bi-directional post2posts plugin to maintain the relationships between posts which works great.

    I have the following relationship, which works great on both ends.

    User (has a bi-diretional relationship with Client) – User
    Client (has bi-directional relationship with User ) – Custom post type
    Report (has bi-directional relationship with Client) – Custom post type

    For example I have 2 users in my database:

    Bill Gates
    Steve Jobs

    and I also have 3 Clients in my database:

    Microsoft
    Apple
    Sony

    I have created a bi-directional relationship between the User
    “Bill Gates” and the Client “Microsoft”
    and also between the User
    “Steve Jobs” and the Client “Apple” and “Sony”.

    ———–
    Summary:
    Steve Jobs – has the Clients Apple and Sony
    Bill Gates – has the Clients Microsoft
    ———–

    Now to the part which I cannot really configure, when the User “Steve Jobs” logs in to write a Report posts (we are in WP backend) I want the Clients “Apple and Sony” to be shown in a meta box.

    How can I achieve this easily ?

    Thanks for hepling me out with this issue.

    regards

  • Hi @zilveer

    Could you please tell me how do you want the client to show up? Do you want it as choices or just information?

    If you want to show it as choices, you can always set the location rules. But if you want to show it as information, you can always use the acf/render_field hook to add it to a custom field meta boxes or use the add_meta_box() function to add it manually.

    To show a relationship value, kindly check our guide here: https://www.advancedcustomfields.com/resources/relationship/.

    Thanks 🙂

  • Hi @james
    I would like it to show up choices, so the User is able to choose a Client and save it to the database.
    I dont know how to use and set the location rules.
    I would be very glad if you/someone could help me to achieve this.

    regards

  • Hi @zilveer

    You should be able to set the relationship field to the Report post type. I’ve attached a screenshot for your reference.

    I hope this helps 🙂

  • Thanks @james for your reply.

    I have already done that, the problem is that ALL Clients is showing up, but I only want the related Clients to show up.

    When Steve Jobs is logged in and wants to add a new Report, then only the following shoud be visible for him:

    Apple
    Sony

    And when Bill Gates is logged in and wants to add a new Report, then only the following shoud be visible for him:

    Microsoft
    ————-
    As it is now, every Client is shown up even though I have relationship between the Clients and the Users.

    Problem is as I can see, when I add a relationship field there is no option to only show relationships for the specific logged in user.
    Or am I doing something wrong?

    Not that I have a bi-directional relationship between the User and Client which works without any problem. I only want the related Clients to show up for the logged in user.

    Thank you very much fpr helping me out of this problem.

    regards

  • Hi again @james

    How can I get in touch with you privately?

    regards

  • This reply has been marked as private.
  • Hi @zilveer

    I’m not really sure about your setup, but I believe that’s how it should be. The relationship should show all options so you can choose any options you want. If you still need to do it, you can try acf/fields/relationship/query hook to filter the result. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/.

    If you don’t know how to code, I suggest you hire a developer to help you out with it, and I’d recommend looking for one on https://studio.envato.com/, https://www.upwork.com/, or https://codeable.io/.

    I hope this helps 🙂

  • Hi @james,
    thank you for oyur reply and helping me out.

    I can some coding but I am not good at WP :/

    I have added this code to the functions.php file:

    function my_relationship_query( $args, $field, $post_id ) {
    	
        // only show children of the current post being edited
        $args['post_parent'] = $post_id;
    	
    	
    	// return
        return $args;
        
    }
    
    // filter for a specific field based on it's name
    add_filter('acf/fields/relationship/query/name=report_relations', 'my_relationship_query', 10, 3);
    
    

    Now I want the report_relations field to only show the following:

    I have a post meta named “p2p_user_to_client_to”, the post_id in the postmeta table refers to the userID and the meta_value refers to a custom post type named “client”.

    So basically I just want the the following in human language:

    Get all the rows in the table for (Logged in user == postmeta.post_id)
    AND
    Get the custom post types WHERE post_type == client and post_type_id == (is whatever meta_value is for the found rows in the postmeta table).

    I dont know really know how to imeplemtn this to the function above since I dont know how WP works.

    If you can help me out I would be very very glad.

    Thanks a lot for your help @james!

    regards

  • Hi again @james,
    I have read and coded this, it was pretty easy to achieve this after som researching.

    The code is below (for others who gets in same situation as me).

    But I still have a problem, I have 2 object_query relationship fields and I would like the second object query relationship field to be based on the first object query relationship, depending on what is chossen in the select field. How can I retrieve the selected field in the function: report_involved_family_relationship ?

    Thanks for helping me out.

    
    /*
     @ This hook allows you to modify the $args array which is used to query the posts shown in the the relationship field list.
     @ Custom field, get related user to Client relations
    */
    function report_involved_client_relationship( $args, $field, $post_id ) {
    	global $wpdb;
    
    	//Query to get related client from post_meta
    	$related_clients = $wpdb->get_results("SELECT meta_value AS user_ids 
             FROM {$wpdb->prefix}postmeta 
             WHERE post_id = ".get_current_user_id()." AND meta_key = 'p2p_user_to_client_to' LIMIT 1");
    	
    	$related_clients_arr = explode(',', $related_clients[0]->user_ids);
    
    	$args = array(
    		'post_type' => 'client',
    		'post__in' => $related_clients_arr
    	);
    
    	
    	// return
        return $args;
        
    }
    
    // filter for a specific field based on it's name
    add_filter('acf/fields/post_object/query/name=report_involved_client', 'report_involved_client_relationship', 10, 3);
    
    /*
     @ This hook allows you to modify the $args array which is used to query the posts shown in the the relationship field list.
     @ Custom field, get related user to family relations
    */
    function report_involved_family_relationship( $args, $field, $post_id ) {
    	global $wpdb;
    
    	
    	echo '<pre>';
    	print_r($field);
    	echo '</pre>';
    	
    	//Query to get related family from post_meta
    	$related_clients = $wpdb->get_results("SELECT meta_value AS family_ids 
             FROM {$wpdb->prefix}postmeta 
             WHERE post_id = ".selected report_involved_client id." AND meta_key = 'p2p_family_to_client_to' LIMIT 1");
    	
    	$related_family_arr = explode(',', $related_clients[0]->user_ids);
    
    	$args = array(
    		'post_type' => 'family',
    		'post__in' => $related_family_arr
    	);
    
    	
    	// return
        return $args;
        
    }
    
    // filter for a specific field based on it's name
    add_filter('acf/fields/post_object/query/name=report_involved_family', 'report_involved_family_relationship', 10, 3);
    
    
Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.