I'm relatively new to Wordpress development and I'm trying to learn more about custom coding.

For starters, I would like to know how to retrieve the date of a custom field and use conditional logic on another date custom field.

For example: If the user chooses a date between Monday and Friday on customfield1, they will not be able to choose Saturday on customfield2.

I would really appreciate it if someone could guide me on doing so!


OK, so you've got a couple of ways to go about this, but first, you need to decide how urgently you want to inform the user that their value is invalid: immediately, or when the post is saved? A general rule of thumb would suggest that earlier is better, but given that you seem to suggest that you're new to custom coding, you may be forced to go later (it might be easier if you want to stick with Wordpress-specific coding). Here's the rundown in any case:

Immediate Validation

This can be done with Javascript/jQuery. Write some code that listens to each of the input's change events, then check if there's already a value in the other input. If so, run your validation logic, if not, wait.

Important note: this solution basically avoids Wordpress development all together. Not necessarily a bad thing, but worth noting.

Late Validation (on post save)

If you're happy to wait until the post is saved to perform validation, you can hook an action to run your validation code. If you're using Advanced Custom Fields, you can use the 'acf/validate_save_post' hook (ref: https://www.advancedcustomfields.com/resources/acf-validate_save_post/). If not, just use the core 'save_post' hook.

Alternate Immediate Validation (untested)

If you're using ACF, it looks like you can use a field-specific hook, like 'acf/validate_value/name=your_date_field_name' (ref: https://www.advancedcustomfields.com/resources/acf-validate_value/). Not around the event life-cycle of ACF well enough to know what's possible with this, so you'll need to look into it, but when the first field's validation event fires, you could store it's value (if you're developing a plugin, a static property would be best, if not, just a global variable) to check on when the second field's validation event fires.

| improve this answer | |
  • Thank you so much for your response. I would like it to be Immediate Validation with Javascript/jQuery, where/how do developers usually find information regarding this? – Terris Aug 30 '19 at 2:02
  • Hmm, depends on your Javascript/jQuery experience. Sounds like you might be new to this, so I'd start by hitting up Google for some basic tutorials, maybe focusing on jQuery, then aim to head towards something like this: sitepoint.com/basic-jquery-form-validation-tutorial which is a fairly simple validation setup, but a good place to start, with room to grow. Good luck. – Jason RWD Aug 30 '19 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.