Support

Account

Home Forums ACF PRO Filter relationship from another relationship field

Helping

Filter relationship from another relationship field

  • I’m curious if this is possible to do. I’ve got a relationship field called module_to_post_filter. I have a module custom post type. I go to that post in that post type and tell this post which page/post it is allowed to be displayed on. Let’s say i’ve selected my “about us” page to accept one of my posts in my module post type.

    I go to edit my “about us” page and i have another relationship field there called “post_to_module”. I want this field to only show the posts I selected from the other field.

    I’m not sure quite how to go about doing this…I tried this, but pretty sure this is wrong because it keeps crashing my wordpress:

    function pageFilter(  $args, $field, $post)
    {
    
      $args[meta_query]= array(
    			array(
          	'key' => 'module_to_post_filter',
            'value' => $post->ID
    				)
    		);
    		return $args;
    }
     
     
    // filter for a specific field based on it's name
    add_filter('acf/fields/relationship/query/name=post_to_module', 'pageFilter', 10, 3);
  • Hi @robbiegod,

    Thanks for the post.

    I would recommend you hook into the acf/load_field filter and use this to pass the selected field object from the other relationship field on the other page.

    The code would look like so:

    <?php
    
    function my_acf_load_field( $field ) {
    	
       //get the field object
        $field['value'] = $new_field_value
        return $field;
        
    }
    
    // key
    add_filter('acf/load_field/key=field_508a263b40457', 'my_acf_load_field');
    
    ?>
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Filter relationship from another relationship field’ is closed to new replies.