Support

Account

Home Forums Backend Issues (wp-admin) Need help from Custom location rules

Solving

Need help from Custom location rules

  • The task was as follows: when selecting a category, it is necessary to look at the value of the get_field('category_type') field in it, and then check whether the category field corresponds to the ‘news’ value. But here’s the bad luck, the check works only after the post is published. Simply put, ajax only gets the category id, but not its fields. What can be done about it?

    
    if (!defined('ABSPATH')) exit;
    
    class My_ACF_Location_Cateroty_Type extends ACF_Location
    {
    
    	public function initialize()
    	{
    		$this->name = 'category_type';
    		$this->label = __("Тип категории", 'acf');
    		$this->category = 'post';
    		$this->object_type = 'post';
    	}
    
    	public function get_values($rule)
    	{
    		return array(
    			'docs' => 'Как документы',
    			'news' => 'Как новости',
    			'gall' => 'Как галерея'
    		);
    	}
    
    	public function match($rule, $screen, $field_group)
    	{
    		if (isset($screen['post_id'])) {
    			$post_id = $screen['post_id'];
    		} else {
    			return false;
    		}
    
    		$category_type_val = $rule['value']; // got 'docs'
    		if (!$category_type_val || is_wp_error($category_type_val)) {
    			return false;
    		}
    
    		$post_terms = wp_get_post_terms($post_id, 'category', array('fields' => 'ids')); // got category ids
    		foreach ($post_terms as $index => $value) { // I'm going through all the category IDs
    			$category_type = get_field('category_type', 'category_' . $value); // Getting the value of the category field
    			if ($category_type) { // If the field exists, I replace the ID value with the field value
    				$post_terms[$index] = $category_type;
    			}
    		}
    
    		$result = (in_array($category_type_val, $post_terms)); // Checking if there is a string in the array == 'docs'
    
    		// Return result taking into account the operator type.
    		if ($rule['operator'] == '!=') {
    			return !$result;
    		}
    		return $result;
    	}
    }
    
    add_action('acf/init', 'my_acf_init_location_types');
    function my_acf_init_location_types()
    {
    	if (function_exists('acf_register_location_type')) {
    		acf_register_location_type('My_ACF_Location_Cateroty_Type');
    	}
    }
    

    P.S. I’m sorry for my English

  • I forgot to write that I use the classic editor

  • I’m a little lost by your question. Is the taxonomy selected using the standard WP taxonomy meta box or is the term only selected in an acf taxonomy field?

    If the term is selected using the standard wp taxonomy meta box then the value selected will be passed to your match function in the $screen arguments. Sorry, it has been a long time since I’ve created location rules and all of my examples use the old method.

    If there the term is set with an acf taxonomy field then it is more difficult because the values of other fields are not sent in the ajax request.

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

You must be logged in to reply to this topic.