Support

Account

Home Forums Backend Issues (wp-admin) Select Values not Being Saved if Select is not Displayed After Post Save

Solving

Select Values not Being Saved if Select is not Displayed After Post Save

  • So this is a bit bizarre, I am using ACFBuilder to create my fields.

    The idea is this: when a new post (adoption) is created, I present with dropdown menus to make their selection. When the user edit this adoption, I don’t want them to be able to select again, those values should never change again.

    So I thought to show the selects on post creation, and when editing, show readonly text fields that display the value of the selects.

    Here is the code:

    		if ( PostHelper::is_current_page_new_post() ) {
    			$afb->add_adoption_type_select_field();
    			$afb->add_adoption_animal_select_field();
    		} else {
    			$afb->add_adoption_type_text_field();
    			$afb->add_adoption_animal_text_field();
    		}
    

    When I code this solution and I select values from the dropdown, the values are not saved in the database.

    If I change the above to (get rid of conditionals):

    
    			$afb->add_adoption_type_select_field();
    			$afb->add_adoption_animal_select_field();
    

    Then after the new post is saved and the selects are displayed again (by this stage we are editing the post) the values are indeed in the database.

    Why would that conditional impact in any way the saving of the fields in the database? I am absolutely baffled.

    Thanks!

  • Well…talk about ugly. What I did was after figuring out if it was a new post or an edit, I created a third tab and placed the selects (that I don’t want the user to see) inside the tab. I then remove the tab by means of jQuery and everything works as expected.

    If anyone has a better solution than this, please chip in. One of my selects calls an endpoint to load its values, I would love to get rid of this overhead when editing the post as that select is no longer needed. Thanks!

    		if ( PostHelper::is_current_page_new_post() ) {
    			$afb->add_adoption_type_select_field();
    			$afb->add_adoption_animal_select_field();
    			$afb->add_adoption_person_select_field();
    		} else {
    			$afb->add_adoption_type_text_field();
    			$afb->add_adoption_animal_text_field();
    			$afb->add_adoption_person_text_field();
    		}
    		$afb->add_tab_field( 'Hidden', 'hidden' );
    		if ( ! PostHelper::is_current_page_new_post() ) {
    			$afb->add_adoption_type_select_field();
    			$afb->add_adoption_animal_select_field();
    			$afb->add_adoption_person_select_field();
    		}
  • The issue could have to do with the way a text field is saved in the DB vs how a select field is saved.

    Also, I don’t know how the builder works, but is the same field key used for both of these fields? A difference in field key could also cause issues.

    If the field should not be changed when editing I would look at making the field read only rather than changing it to a test field.

  • @hube2 thanks for the reply.

    ACFBuilder generate different keys for all of the controls, so that’s not the case.

    I looked at making the field read only, but it all looked complicated since it’s a select2 field. Maybe I’ll persevere if I have the time.

    (update: found this link, traviskelleher posted something that might work)

    Thanks!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.