Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adds ACF Field Group location rule for Post Type Support.
<?php
/**
* Plugin Name: Field Group Location: Post Type Supports
* Plugin URI: http://qwp6t.me/acf-post-type-supports
* Description: Adds ACF Field Group location rule for Post Type Support. NOTE: You must first declare the supported feature in your theme.
* Version: 1.0.0
* Author: QWp6t
* Author URI: http://qwp6t.me
* License: MIT License
*/
add_filter('acf/location/rule_types', function ($choices) {
$choices[__('Post', 'acf')]['post_type_supports'] = __('Post Type Supports', 'qwp6t');
return $choices;
});
add_filter('acf/location/rule_values/post_type_supports', function ($choices = []) {
foreach ($GLOBALS['_wp_post_type_features'] as $post_type) {
foreach ($post_type as $feature => $args) {
$choices[$feature] = $feature;
}
}
$choices = array_unique($choices);
asort($choices);
return $choices;
}, 10, 1);
add_filter('acf/location/rule_match/post_type_supports', function ($match, array $rule, array $options) {
if (!$options['post_id'] || $match) {
return false;
}
$post_type = $options['post_type'] ?: get_post_type($options['post_id']);
$match = post_type_supports($post_type, $rule['value']);
return ($rule['operator'] == "!=") ? !$match : $match;
}, 10, 3);
@QWp6t

This comment has been minimized.

Copy link
Owner Author

commented Sep 14, 2015

Add this somewhere in your theme...

add_post_type_support('page', 'hero');

The hero designation is arbitrary. Just make up a name for whatever feature you're creating. Typically you would want it to correspond to the Field Group name in ACF. Hero was just the example I initially gave.

post-type-supports-rule

@kalenjohnson

This comment has been minimized.

Copy link

commented Sep 14, 2015

🎵 did you ever know that you're my hero

@kevinwhoffman

This comment has been minimized.

Copy link

commented Sep 16, 2015

@QWp6t I don't think I'm fully understanding the use case. Could you elaborate on the advantages of using this method versus using multiple rules based on Post Type?

For example if both Posts and Pages supported hero, could I not create the same result with two rules for "If Post Type is equal to Post" and "If Post Type is equal to Page"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.