Support

Account

Forum Replies Created

  • I have used the filter inside a public function so that I can have it on my plugin folder. Should I use it without any function?

  • I will try this.But with what action should I trigger the php function?
    With do_action(‘init’)?

  • In my functions.php:

    
    function fill_data($post_id){
    	foreach (['sub_title'] as $name) {
    
    	add_filter( 'acf/fields/post_object/query/name=' . $name, function ( $args, $field, $post_id ) {
    
    		$list = $_POST['your_value']  ?? get_field( 'sub_title', $post_id );
    		
    
    		// change the query according to your model
    		if ( null !== $list ) {
    			// $args['tax_query']
    			// $args['meta_query'] 
    		}
    
    		return $args;
    	}, 1, 3 );
    }
    	
    }
    
    function my_scripts_method() {
        wp_enqueue_script(
            'script',
            get_stylesheet_directory_uri() . 'script.js', #your JS file
            array( 'jquery' ) #dependencies
        );
    }
    
    add_action( 'wp_enqueue_scripts', 'my_scripts_method' );
    	
    add_action('init', 'fill_data', 10, 1);

    In my script.js (in the same folder with functions.php):

    jQuery(document).ready(($) => {
        acf.addFilter('select2_ajax_data',(data, args, $input, field, instance) => {
                
                // Get the combo field instance
    
                let your_combo = acf.getFields({
                    name: 'sub_title'
                })[0];
    
                if (your_combo !== undefined && (data.field_key === field.get('key')) {
                        data.your_value = your_combo.val();  // Here is the magic ;)
                    }
                }
            });
    })

    It doesn;t seem to work. I am not sure if I haven’t included correctly the script.js file. Also shouldn’t the php function to be triggered every time I change my choice in my select drop down field? Thank you

  • I am kind of trouble in the first step.
    Can you explain me a bit more how can I pass the selected value to ajax?

  • I used this add_action( 'acf/prepare_field/key=field_1', 'xml_data' ); instead of wp_inser_post and it seems to work fine. And it also seems to work with both load and prepare field. Thank you again!

  • I am getting the data from an xml file. When I start a new post,
    I run the function, so I can read the xml and process the data I need and finally push them in an array.
    And I want to display this array as the field options.
    If you think there is another way, I will be happy to try it.

  • Ok, I see.
    So should I change my code to this:

    function xml_data(){
    $data_array; //my array with my data
    $field = get_field_object('field_1');
    $field['choices'] = array();
    foreach($$data_array as $choice){
    	$field['choices'][ $choice ] = $choice;
      }
    
    return $field;
    }
    
    add_action('wp_insert_post', 'xml_data');
    apply_filters( 'acf/prepare_field', 'xml_data' );

    Could it work that way?
    Thank you

  • As you can see, I am already use the get_field_object() with the key field (and if I print I get the right object with the right choices). Should I just added the
    acf_update_field($field) after my for loop?

    Thank you

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