Home › Forums › Backend Issues (wp-admin) › 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’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.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.