Skip to content

Instantly share code, notes, and snippets.

@nocean
Created January 16, 2016 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nocean/1c13c545d501f5da07b7 to your computer and use it in GitHub Desktop.
Save nocean/1c13c545d501f5da07b7 to your computer and use it in GitHub Desktop.
/**
* Add a custom location rule for Comment Type
*/
add_filter('acf/location/rule_types', 'THEMENAME_acf_location_rules_types');
function THEMENAME_acf_location_rules_types( $choices ) {
// This creates a new group in the Location dropdown called "Comments"
// And adds a child-item called "Comments on Post Type"
$choices['Comments']['comments_on_post_type'] = 'Comments on Post Type';
return $choices;
}
/**
* Populate the custom location rule's choices
*/
add_filter('acf/location/rule_values/comments_on_post_type', 'THEMENAME_acf_location_rules_values_user');
function THEMENAME_acf_location_rules_values_user( $choices ) {
// Get all the "public" post types.
$post_types = get_post_types(array('public' => true));
// Assuming there are some, loop through and add to the $choices array.
if( $post_types ) {
foreach( $post_types as $post_type ) {
$choices[ $post_type ] = $post_type;
}
}
return $choices;
}
/*
* Perform the match
*/
add_filter('acf/location/rule_match/comments_on_post_type', 'THEMENAME_acf_location_rules_match_comment_post_type', 10, 3);
function THEMENAME_acf_location_rules_match_comment_post_type( $match, $rule, $options ) {
global $post;
// Get the post type of the current page, as well as the one we've selected
$current_post_type = $post->post_type;
$selected_post_type = $rule['value'];
// Depending on your operator, compare and return a boolean for ACF to use.
if ($rule['operator'] == "==") {
$match = ( $current_post_type == $selected_post_type );
} elseif ($rule['operator'] == "!=") {
$match = ( $current_post_type != $selected_post_type );
}
return $match;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment