Conditional field value on CPT archive
-
I’m using ACFE’s CPT archive feature. On an archive admin page, I have two fields with the same field name. They are conditional fields and only one is ever shown at a time. The second field, even if it’s hidden, returns its value, rather than returning null, and overrides the first field that’s visible. This isn’t how ACF works on non-archive pages. The hidden field returns NULL.
-
This topic was modified 10 months, 3 weeks ago by
joshkern.
-
This topic was modified 10 months, 3 weeks ago by
-
Hello,
Thanks for the feedback!
I’m not sure to fully understand the issue, can you please provide more information, like a screenshot or video? Which function doesn’t return
NULL
? What do you mean by “overrides the first field that’s visible”?Please note that when using
get_field('my_field')
, ACF will always return the value of the field from the database, being it hidden by Conditional Logic or not.NULL
in theget_field()
logic means there is no value in the database for the said field.You can use the Developer Mode if you want to get a quick view of the meta saved on each post/options pages, if that helps.
Thanks!
Regards.
I have two fields with the same field name. Let’s call them “Same Field” with the “same_field” field name. They use conditional logic and are visible/hidden based on other field options. Only one is shown at a time.
In my scenario, the first “Same Field” is visible. When I save the CPT archive, I would assume the visible “Same Field” value would be saved to the database. However, this is not the case. The conditionally hidden “Same Field” value is saved instead.
The value comes from the conditionally hidden field because it’s second in order.
While ACF may return the value whether the field is hidden or not, I assume ACF saves the value from the visible field. I don’t have this issue with ACF fields on other pages or ACF blocks.
Hello,
I tried using a setup similar to what you described, and I wasn’t able to reproduce the issue using the latest WP/ACF/ACFE versions. Here is the video of my test. Is there something different on your environment? Maybe a hook?
In fact, as you pointed out, Fields that are hidden by Conditional Logic are disabled, and are not sent during the form submission. So the hidden field should not be saved, even if it comes after the first one, as we can see on the test video.
Here is the PHP code with the Field Group / Post Type / Options Page that I used in my test, so you can try it yourself:
// post type add_action('init', function(){ register_post_type('my-post-type', array( 'name' => 'my-post-type', 'label' => 'My Post Type', 'active' => true, 'description' => '', 'hierarchical' => false, 'supports' => array( 'title', 'editor', ), 'taxonomies' => array( 'colors', ), 'public' => true, 'exclude_from_search' => false, 'publicly_queryable' => true, 'can_export' => true, 'delete_with_user' => null, 'labels' => array(), 'menu_position' => '', 'menu_icon' => 'dashicons-admin-post', 'show_ui' => true, 'show_in_menu' => true, 'show_in_nav_menus' => true, 'show_in_admin_bar' => true, 'has_archive' => true, 'rewrite' => true, 'capability_type' => 'post', 'capabilities' => array(), 'map_meta_cap' => null, 'show_in_rest' => false, 'rest_base' => '', 'rest_controller_class' => 'WP_REST_Posts_Controller', 'acfe_archive_template' => '', 'acfe_archive_ppp' => 10, 'acfe_archive_orderby' => 'date', 'acfe_archive_order' => 'DESC', 'acfe_archive_meta_key' => '', 'acfe_archive_meta_type' => '', 'acfe_single_template' => '', 'acfe_admin_archive' => true, 'acfe_admin_ppp' => 10, 'acfe_admin_orderby' => 'date', 'acfe_admin_order' => 'DESC', 'acfe_admin_meta_key' => '', 'acfe_admin_meta_type' => '', )); }); // options page add_action('acf/init', function(){ acf_add_options_page(array( 'menu_slug' => 'my-options-page', 'page_title' => 'My Options Page', 'active' => true, 'menu_title' => '', 'capability' => 'edit_posts', 'parent_slug' => '', 'position' => '', 'icon_url' => '', 'redirect' => true, 'post_id' => 'my-options', 'autoload' => false, 'update_button' => 'Update', 'updated_message' => 'Options Updated', )); }); // field group add_action('acf/init', function(){ acf_add_local_field_group( array( 'key' => 'group_6630a3c43535d', 'title' => 'Page', 'fields' => array( array( 'key' => 'field_6632ad0d3996b', 'label' => 'Select', 'name' => 'select', 'aria-label' => '', 'type' => 'select', 'instructions' => '', 'required' => 0, 'conditional_logic' => 0, 'wrapper' => array( 'width' => '', 'class' => '', 'id' => '', ), 'choices' => array( 'same_field1' => 'Show Same Field (1)', 'same_field2' => 'Show Same Field (2)', ), 'default_value' => false, 'return_format' => 'value', 'multiple' => 0, 'allow_custom' => 0, 'placeholder' => '', 'search_placeholder' => '', 'allow_null' => 0, 'ui' => 0, 'ajax' => 0, ), array( 'key' => 'field_6630a3e031693', 'label' => 'Same Field (1)', 'name' => 'same_field', 'aria-label' => '', 'type' => 'textarea', 'instructions' => '', 'required' => 0, 'conditional_logic' => array( array( array( 'field' => 'field_6632ad0d3996b', 'operator' => '==', 'value' => 'same_field1', ), ), ), 'wrapper' => array( 'width' => '', 'class' => '', 'id' => '', ), 'default_value' => '', 'acfe_textarea_code' => 0, 'maxlength' => '', 'rows' => '', 'placeholder' => '', 'new_lines' => '', ), array( 'key' => 'field_6630a54291afa', 'label' => 'Same Field (2)', 'name' => 'same_field', 'aria-label' => '', 'type' => 'textarea', 'instructions' => '', 'required' => 0, 'conditional_logic' => array( array( array( 'field' => 'field_6632ad0d3996b', 'operator' => '==', 'value' => 'same_field2', ), ), ), 'wrapper' => array( 'width' => '', 'class' => '', 'id' => '', ), 'default_value' => '', 'acfe_textarea_code' => 0, 'maxlength' => '', 'rows' => '', 'placeholder' => '', 'new_lines' => '', ), ), 'location' => array( array( array( 'param' => 'post_type_archive', 'operator' => '==', 'value' => 'my-post-type', ), ), array( array( 'param' => 'options_page', 'operator' => '==', 'value' => 'my-options-page', ), ), ), 'menu_order' => 0, 'position' => 'normal', 'style' => 'default', 'label_placement' => 'left', 'instruction_placement' => 'label', 'hide_on_screen' => array( 0 => 'the_content', ), 'active' => true, 'description' => '', 'show_in_rest' => 0, ) ); });
Let me know if you managed to reproduce the issue with the test code.
Thanks!
Regards.
- The topic ‘Conditional field value on CPT archive’ is closed to new replies.