Support

Account

Home Forums Feature Requests use meta data for select2_ajax_data

Helping

use meta data for select2_ajax_data

  • Hello,

    I have a project where I needed to filter a taxonomy field by another taxonomy field with select2_ajax_data (more precisely, the meta value equivalent to that tax field), for that, I had to add meta key and value filtering to the function get_ajax_query in the acf/fields/taxonomy.php file, is it possible to add it in future updates?
    here is the whole new function:

    	function get_ajax_query( $options = array() ) {
    		
       		// defaults
       		$options = acf_parse_args($options, array(
    			'post_id'		=> 0,
    			's'				=> '',
    			'field_key'		=> '',
    			'paged'			=> 0,
    			'meta_key' 			=> '',
    			'meta_value' 			=> '',
    		));
    		
    		
    		// load field
    		$field = acf_get_field( $options['field_key'] );
    		if( !$field ) return false;
    		
    		
    		// bail early if taxonomy does not exist
    		if( !taxonomy_exists($field['taxonomy']) ) return false;
    		
    		
    		// vars
       		$results = array();
    		$is_hierarchical = is_taxonomy_hierarchical( $field['taxonomy'] );
    		$is_pagination = ($options['paged'] > 0);
    		$is_search = false;
    		$limit = 20;
    		$offset = 20 * ($options['paged'] - 1);
    		
    		
    		// args
    		$args = array(
    			'taxonomy'		=> $field['taxonomy'],
    			'hide_empty'	=> false
    		);
    		
    		
    		// pagination
    		// - don't bother for hierarchial terms, we will need to load all terms anyway
    		if( $is_pagination && !$is_hierarchical ) {
    			
    			$args['number'] = $limit;
    			$args['offset'] = $offset;
    		
    		}
    		
    		
    		// search
    		if( $options['s'] !== '' ) {
    			
    			// strip slashes (search may be integer)
    			$s = wp_unslash( strval($options['s']) );
    			
    			
    			// update vars
    			$args['search'] = $s;
    			$is_search = true;
    			
    		}
    
    		// meta: these are the lines that have been added
    		if( $options['meta_key'] !== '' ) {			
    			
    			
    			$args['meta_key'] = $options['meta_key'];			
    		}
    
    		if( $options['meta_value'] !== '' ) {			
    			
    			
    			$args['meta_value'] = $options['meta_value'];			
    		}
                    // end of meta
    		
    		
    		// filters
    		$args = apply_filters('acf/fields/taxonomy/query', $args, $field, $options['post_id']);
    		$args = apply_filters('acf/fields/taxonomy/query/name=' . $field['name'], $args, $field, $options['post_id'] );
    		$args = apply_filters('acf/fields/taxonomy/query/key=' . $field['key'], $args, $field, $options['post_id'] );
    		
    		
    		// get terms
    		$terms = acf_get_terms( $args );
    		
    		
    		// sort into hierachial order!
    		if( $is_hierarchical ) {
    			
    			// update vars
    			$limit = acf_maybe_get( $args, 'number', $limit );
    			$offset = acf_maybe_get( $args, 'offset', $offset );
    			
    			
    			// get parent
    			$parent = acf_maybe_get( $args, 'parent', 0 );
    			$parent = acf_maybe_get( $args, 'child_of', $parent );
    			
    			
    			// this will fail if a search has taken place because parents wont exist
    			if( !$is_search ) {
    				
    				// order terms
    				$ordered_terms = _get_term_children( $parent, $terms, $field['taxonomy'] );
    				
    				
    				// check for empty array (possible if parent did not exist within original data)
    				if( !empty($ordered_terms) ) {
    					
    					$terms = $ordered_terms;
    					
    				}
    			}
    			
    			
    			// fake pagination
    			if( $is_pagination ) {
    				
    				$terms = array_slice($terms, $offset, $limit);
    				
    			}
    			
    		}
    		
    		
    		/// append to r
    		foreach( $terms as $term ) {
    		
    			// add to json
    			$results[] = array(
    				'id'	=> $term->term_id,
    				'text'	=> $this->get_term_title( $term, $field, $options['post_id'] )
    			);
    			
    		}
    		
    		
    		// vars
    		$response = array(
    			'results'	=> $results,
    			'limit'		=> $limit
    		);
    		
    		
    		// return
    		return $response;
    			
    	}

    Thank you.

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

The topic ‘use meta data for select2_ajax_data’ is closed to new replies.