Support

Account

Home Forums Front-end Issues Display parent\'s relationship on child post

Solved

Display parent\'s relationship on child post

  • I have a heirarchal ‘Customer’ CPT in which the ‘Employees’ are it’s children.

    The Parent posts (customers) are joined with several other CPT’s through relationship fields.

    I have created an Order form that is displayed on the child posts (employees) posts.

    Within the form is a select post object field. On the parent post (Customer) I am able to filter the relationship with the following:

    function my_post_object_query($args, $field, $post_id) {
    
    	$meta_query = array(
    		array(
    		'key' => 'pro_to_con',
    		'value' => $post_id,
    		'compare' => 'LIKE'
    	)
    );
    	$args['meta_query'] = $meta_query;
    		return $args;
    }
    add_filter('acf/fields/post_object/query/key=field_60a3cfc87e608', 'my_post_object_query', 10, 3);

    However, this obviously does not work on the child post, since the post value I need is the parent of the current post id.

    I’ve searched the forums, dug into code ex, tried everything imaginable. I’m sure it must be so simple, but I’m struggling with the answer.

    I would greatly appreciate any advice.

  • If you want to get the relationship from the parent post id the you

    
    $value = get_field('field_name', $post->post_parent);
    
  • John,

    Your answers to other ACF user’s questions have helped me tremendously in learning ACF. I’m a fan of yours and I appreciate your reply.

    I tried your suggestion of

    $value = get_field('field_name', $post->post_parent);

    and it’s still displaying all of the posts – including those without a relationship to the parent. I apologize, I’m still a bit lost, so I wonder if I explained my question properly.

    The post object field on the form on the child post should filter posts that share a relationship with the child post’s parent.

    I’m still missing something…

  • I have a feeling that I am not completely understanding how the relationships between your post types and posts work.

    But I think I’ve worked out what you want to do

    
    function my_post_object_query($args, $field, $post_id) {
    	$parent = wp_get_post_parent_id($post_id);
    	$meta_query = array(
    		array(
    		'key' => 'pro_to_con',
    		'value' => $parent,
    		'compare' => 'LIKE'
    	)
    );
    	$args['meta_query'] = $meta_query;
    		return $args;
    }
    
  • Prior to reaching out for support, I tried logic similar to your answer above 100x, but with slight variations in the format. I’ve found that the proper format can be so nuanced.

    Your code works. Thank you, John. You may not know how much people rely upon you and how helpful you are to them.

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

You must be logged in to reply to this topic.