Home › Forums › General Issues › Display different field groups on New and Edit Post
I am trying to add different field groups on new-post screen and edit post screens.
How this can be done?
I tried to add custom rules. But, no luck.
My code is as below:
<?php
// Add custom types
add_filter('acf/location/rule_types', 'acf_location_rules_types_postmode');
function acf_location_rules_types_postmode( $choices ){
$choices['Basic']['postmode'] = 'Post Mode';
return $choices;
}
// Add custom values
add_filter('acf/location/rule_values/postmode', 'acf_location_rules_values_postmode');
function acf_location_rules_values_postmode( $choices )
{
$postmodes = array(
'post_new' => 'Post New',
'post_edit' => 'Post Edit',
);
$new_choices = array();
if( $postmodes )
{
foreach( $postmodes as $postmode => $postmodes_name )
{
$new_choices[ $postmode ] = $postmodes_name;
}
}
return $new_choices;
}
// Matching the rule
add_filter('acf/location/rule_match/postmode', 'acf_location_rules_match_postmode', 10, 3);
function acf_location_rules_match_postmode( $match, $rule, $options )
{
global $pagenow, $current_screen;
$current_user = wp_get_current_user();
$rule_value = $rule['value'];
if($rule['operator'] == "==")
{
if( $rule_value = 'post_edit' || $pagenow == 'post-new.php' ) $match = true;
if( $rule_value = 'post_new' || $pagenow == 'post.php' ) $match = true;
}
elseif($rule['operator'] == "!=")
{
if( $rule_value = 'post_edit' || $pagenow == 'post-new.php' ) $match = false;
if( $rule_value = 'post_new' || $pagenow == 'post.php' ) $match = false;
}
return $match;
}
?>
I’m a little lost about why you’d want to display different field groups on new or existing posts. There would be some problems with doing this because, even if you name the fields the same, the field keys for the fields in each group will be different and could cause problems saving an retrieving values from the database.
However, that being said, the problems are in your filter match function
single =
instead of double ==
and ||
instead of &&
and I altered the order the values checked in the!=
section
// Matching the rule
add_filter('acf/location/rule_match/postmode', 'acf_location_rules_match_postmode', 10, 3);
function acf_location_rules_match_postmode( $match, $rule, $options )
{
global $pagenow;
$rule_value = $rule['value'];
if ($rule['operator'] == '==') {
if ($rule_value == 'post_new' && $pagenow == 'post-new.php') $match = true;
if ($rule_value == 'post_edit' && $pagenow == 'post.php' ) $match = true;
} elseif($rule['operator'] == '!=') {
if ($rule_value == 'post_edit' && $pagenow == 'post-new.php') $match = true;
if ($rule_value == 'post_new' && $pagenow == 'post.php') $match = true;
}
return $match;
}
Thanks @hube2,
Actually, I am working on a project which require to display custom-post (data submitted from front) in view-mode in specific structure. I created custom field-type to display data. Also there is requirement to add data from back-end.
So, I created 2 field-sets to display in three different modes.
The topic ‘Display different field groups on New and Edit Post’ is closed to new replies.
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.