Add Post Meta Category Rule on ACF

Kadang kala kita ingin menambahkan custom rule pada group field ACF, tambahkan baris kode berikut ke functions.php pada template

add_filter('acf/location/rule_types', function ($choices) {
	$choices['Post']['post_meta_custom'] = 'Post Meta';
	return $choices;
});

add_filter('acf/location/rule_values/post_meta_custom', function ($choices) {
	//List custom post meta rule
	$choices['_syncplugin_expert_product'] = 'Sync Plugin Expert Product';
	$choices['_syncplugin_kos_product'] = 'Sync Plugin Kosatec Product';

	return $choices;
});

add_filter('acf/location/rule_match/post_meta_custom', function ($match, $rule, $options) {
	if (!isset($options['post_id'])) {
		return false;
	}

	$meta_value = get_post_meta($options['post_id'], $rule['value'], true);

	if ($rule['operator'] == '==') {
		$match = $meta_value !== '';
	} elseif ($rule['operator'] == '!=') {
		$match = $meta_value === '';
	}

	return $match;
}, 10, 3);

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan.