Title: Conditional Logic based on post data field
Last modified: March 17, 2024

---

# Conditional Logic based on post data field

 *  Resolved [sitenerds](https://wordpress.org/support/users/sitenerds/)
 * (@sitenerds)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/conditional-logic-based-on-post-data-field/)
 * Hi,
 * I wanted to know the possibility of having a field having conditional logic based
   on a specific field in the Post Data form field.
 * For example, I am building a Business Directory with a custom post type of “Listing”.
   The listings has a categories field with in, which can have the business category
   e.g. Restaurant, Plumber etc.
 * [https://ibb.co/6X39Qyd](https://ibb.co/6X39Qyd)
 * ![](https://ibb.co/6X39Qyd)
 * I want a Number field to be able to have conditional logic to show only if the
   business category is Restaurant. Is there any way to implement this? Currently
   it only gives me the option to put conditional logic based on the whole post (
   see screenshot)
 * [https://ibb.co/r3jQ6N3](https://ibb.co/r3jQ6N3)
 * ![](https://ibb.co/r3jQ6N3)
 * [https://ibb.co/PwnWR1z](https://ibb.co/PwnWR1z)
 * ![](https://ibb.co/PwnWR1z)

Viewing 15 replies - 1 through 15 (of 22 total)

1 [2](https://wordpress.org/support/topic/conditional-logic-based-on-post-data-field/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/conditional-logic-based-on-post-data-field/page/2/?output_format=md)

 *  [Kris – WPMU DEV Support](https://wordpress.org/support/users/wpmudevsupport13/)
 * (@wpmudevsupport13)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/conditional-logic-based-on-post-data-field/#post-17506780)
 * Hi [@sitenerds](https://wordpress.org/support/users/sitenerds/)
 * I hope you are doing well today.
 * By default, this will be not possible. I pinged out SLS Team to review this and
   see what will be possible in this case. We will post an update here as soon as
   more information is available.
 * Kind Regards,
   Kris
 *  [Kris – WPMU DEV Support](https://wordpress.org/support/users/wpmudevsupport13/)
 * (@wpmudevsupport13)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/conditional-logic-based-on-post-data-field/#post-17509026)
 * Hi again [@sitenerds](https://wordpress.org/support/users/sitenerds/)
 * This snippet should help
 *     ```wp-block-code
       add_action('wp_footer', 'wpmudev_number_field_visibility_category', 9999 );
       function wpmudev_number_field_visibility_category() {
           global $post;
           if ( is_a( $post, 'WP_Post' ) && ! has_shortcode( $post->post_content, 'forminator_form' ) ) {
               return;
           }
           ?>
       	<script type="text/javascript">
       	jQuery(document).ready(function($) {
               setTimeout(function() {
       			$('.forminator-custom-form').trigger('after.load.forminator');
               }, 100);
   
               $(document).on('after.load.forminator', function(e, form_id) {
                   if ( e.target.id == 'forminator-module-1609' ) { // Please change the form ID.
                       $('select[name="postdata-1-category"]').on('select2:select', function(ev) {
       					var select_val = $(ev.currentTarget).val();
                           if ( 3 == select_val ) { // Please change 3 to your category ID.
                               $('#number-1').removeClass('forminator-hidden');
                           } else {
                               $('#number-1').addClass('forminator-hidden');
                           }
       				});
                   }
               });
           });
           </script>
           <?php
       }
       ```
   
 * Note to change 1609 to your form ID and 3 to your category ID.
 * He is a guide on how to install mu-plugin:
   [https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins](https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins)
 * Kind Regards,
   Kris
 *  Thread Starter [sitenerds](https://wordpress.org/support/users/sitenerds/)
 * (@sitenerds)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/conditional-logic-based-on-post-data-field/#post-17512380)
 * Hi Kris,
 * Thanks for looking into this. I tried the snippet but it didn’t seem to work.
   Could you double-check that it is correct?
 * For clarity, I separated the JavaScript and PHP codes and input them separately
   using the WP Code Snippets plugin.
 *  Plugin Support [Nebu John – WPMU DEV Support](https://wordpress.org/support/users/wpmudevsupport14/)
 * (@wpmudevsupport14)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/conditional-logic-based-on-post-data-field/#post-17531397)
 * Hi [@sitenerds](https://wordpress.org/support/users/sitenerds/),
 * Apologies for the delay in responding. It seems that the response was not notified
   on our end for some reason.
 * I verified the code on a test website at my end and could confirm it works fine.
 * Can you please try adding the code using a mu-plugin as suggested instead of 
   using the WP Code Snippets plugin?
 * Please let us know the result.
 * Kind Regards,
    Nebu John
 *  Thread Starter [sitenerds](https://wordpress.org/support/users/sitenerds/)
 * (@sitenerds)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/conditional-logic-based-on-post-data-field/#post-17532496)
 * Hi Nebu,
 * No worries, thanks for the reply.
 * I implemented the code as a mu-plugin as instructed, changed the form ID and 
   added in my category ID and it still did not change anything. I still get the
   same options in the conditional logic: [https://ibb.co/ykk05dp](https://ibb.co/ykk05dp)
 *  Plugin Support [Nebu John – WPMU DEV Support](https://wordpress.org/support/users/wpmudevsupport14/)
 * (@wpmudevsupport14)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/conditional-logic-based-on-post-data-field/#post-17536749)
 * Hi [@sitenerds](https://wordpress.org/support/users/sitenerds/),
 * Sorry to know that the workaround is still not working at your end.
 * Can you please try disabling the option “Load form using AJAX” under the Behaviour
   tab of the form and check if that helps?
 * If the issue persists, please provide an export of the form using a cloud service
   like Google Drive or pastebin.com so that we can take a closer look.
 * Please find detailed instructions on how to export a form in our documentation
   here: [https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#import-export](https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#import-export)
 * Kind Regards,
    Nebu John
 *  Plugin Support [Amin – WPMU DEV Support](https://wordpress.org/support/users/wpmudev-support2/)
 * (@wpmudev-support2)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/conditional-logic-based-on-post-data-field/#post-17549574)
 * Hello [@sitenerds](https://wordpress.org/support/users/sitenerds/) ,
 * It’s been several days since we asked for the above test result and without the
   response we are not able to provide further assistance.
 * If you require further help please feel free to reopen this topic.
 * kind regards,
    Kasia
 *  Thread Starter [sitenerds](https://wordpress.org/support/users/sitenerds/)
 * (@sitenerds)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/conditional-logic-based-on-post-data-field/#post-17551756)
 * Hi [@wpmudev-support2](https://wordpress.org/support/users/wpmudev-support2/)/
   [@wpmudevsupport14](https://wordpress.org/support/users/wpmudevsupport14/)
 * Loading form with AJAX was unchecked and I still am not able to select the category
   in the conditional logic.
 * Please see export here: [https://docs.google.com/document/d/1IWg5EcKrb_-KNPXnH4bucQiErzRp_BZVXF6n_DvwREU/edit?usp=drive_link](https://docs.google.com/document/d/1IWg5EcKrb_-KNPXnH4bucQiErzRp_BZVXF6n_DvwREU/edit?usp=drive_link)
    -  This reply was modified 2 years, 2 months ago by [sitenerds](https://wordpress.org/support/users/sitenerds/).
 *  Plugin Support [Nithin – WPMU DEV Support](https://wordpress.org/support/users/wpmudevsupport11/)
 * (@wpmudevsupport11)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/conditional-logic-based-on-post-data-field/#post-17554107)
 * Hi [@sitenerds](https://wordpress.org/support/users/sitenerds/),
 * It appears the Google Drive URL is private, so we couldn’t access the export.
   Please make the URL public, so that we can check further.
 * Looking forward to your response.
 * Best Regards,
 * Nithin
 *  Thread Starter [sitenerds](https://wordpress.org/support/users/sitenerds/)
 * (@sitenerds)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/conditional-logic-based-on-post-data-field/#post-17554175)
 * Hi [@wpmudevsupport11](https://wordpress.org/support/users/wpmudevsupport11/)
 * Here you are: [https://docs.google.com/document/d/1IWg5EcKrb_-KNPXnH4bucQiErzRp_BZVXF6n_DvwREU/edit?usp=sharing](https://docs.google.com/document/d/1IWg5EcKrb_-KNPXnH4bucQiErzRp_BZVXF6n_DvwREU/edit?usp=sharing)
 *  Plugin Support [Dmytro – WPMU DEV Support](https://wordpress.org/support/users/wpmudevsupport16/)
 * (@wpmudevsupport16)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/conditional-logic-based-on-post-data-field/#post-17557898)
 * Hello [@sitenerds](https://wordpress.org/support/users/sitenerds/),
 * Thank you for sharing the form. Could you please try the following snippet, instead
   of the previous one:
 *     ```
       <?php
       add_action(
       	'wp_head',
       	function() {
               global $post;
               if ( is_a( $post, 'WP_Post' ) && ! has_shortcode( $post->post_content, 'forminator_form' ) ) {
                   return;
               }
   
               $form_id = 1609;
               $cat_id = 3;
               $cond_field = 'number-1';
   
       		?>
       		<script type="text/javascript">
       			jQuery(document).bind("ready ajaxComplete", function(){
       				if(jQuery('#forminator-module-<?php echo $form_id; ?> select[name="postdata-1-category"]').val() == <?php echo $cat_id; ?>) {
       					jQuery("#<?php echo $cond_field; ?>").removeClass("forminator-hidden");
       				} else {
       					jQuery("#<?php echo $cond_field; ?>").addClass("forminator-hidden");
       				}
       				jQuery('#forminator-module-<?php echo $form_id; ?> select[name="postdata-1-category"]').bind("select2:select", function(e){
       					if(jQuery(this).val() == <?php echo $cat_id; ?>) {
       						jQuery("#<?php echo $cond_field; ?>").removeClass("forminator-hidden");
       					} else {
       						jQuery("#<?php echo $cond_field; ?>").addClass("forminator-hidden");
       					}
       				});
       			});
               </script>;
   
       		<?php
       	}
       );
       ```
   
 * – replace the form and category IDs on these lines:
 *     ```
               $form_id = 1609;
               $cat_id = 3;
       ```
   
 * Hope this helps. Please let us know if there’s still any issue.
 * Best Regards,
    Dmytro
 *  Thread Starter [sitenerds](https://wordpress.org/support/users/sitenerds/)
 * (@sitenerds)
 * [2 years, 1 month ago](https://wordpress.org/support/topic/conditional-logic-based-on-post-data-field/#post-17683658)
 * Hello again,
 * I am still getting the same output. The options in for conditional logic do not
   show the post category: [https://ibb.co/PwnWR1z](https://ibb.co/PwnWR1z)
 *  Plugin Support [Dmytro – WPMU DEV Support](https://wordpress.org/support/users/wpmudevsupport16/)
 * (@wpmudevsupport16)
 * [2 years, 1 month ago](https://wordpress.org/support/topic/conditional-logic-based-on-post-data-field/#post-17685522)
 * Hello [@sitenerds](https://wordpress.org/support/users/sitenerds/),
 * Please note, that the snippet doesn’t add any user interface elements for the
   conditional logic (this would require additional programming, which is beyond
   our support scope). I’m sorry for any confusion regarding this.
 * Instead, it hides/shows the field programmatically, depending on the category
   which a user selects on the front end.
 * > The options in for conditional logic do not show the post category
 * You’ll need to specify the ID of the _Restaurant_ category. This ID can be found
   in the address bar, when you open this category for editing:
    [https://prnt.sc/CM9YT2Egnxd5](https://prnt.sc/CM9YT2Egnxd5)
 * Could you please check if the form and category IDs in the code have been replaced
   with the correct values, and then try selecting a category in the form after 
   publishing it.
 * Please let us know if there’s still any issue.
 * Best Regards,
    Dmytro
    -  This reply was modified 2 years, 1 month ago by [Dmytro - WPMU DEV Support](https://wordpress.org/support/users/wpmudevsupport16/).
 *  Thread Starter [sitenerds](https://wordpress.org/support/users/sitenerds/)
 * (@sitenerds)
 * [2 years, 1 month ago](https://wordpress.org/support/topic/conditional-logic-based-on-post-data-field/#post-17705094)
 * Hello,
 * Thanks for clarifying. So the number field correctly is hidden on the front-end
   when the snippet is implemented, however it does not seem to be showing when 
   I have the category in the post selected.
 * I went into the category and found the id 50 from the following url: term.php?
   taxonomy=hp_listing_category&tag_ID=50&post_type=hp_listing
 * Having replaced this in the snippet, the the field doesn’t show up as expected
   with the category selected: [https://ibb.co/C5pHHqR](https://ibb.co/C5pHHqR)
 * The field doesn’t appear with any of the other categories selected either (tested
   incase there was an issue with the id).
 *  Plugin Support [Nithin – WPMU DEV Support](https://wordpress.org/support/users/wpmudevsupport11/)
 * (@wpmudevsupport11)
 * [2 years, 1 month ago](https://wordpress.org/support/topic/conditional-logic-based-on-post-data-field/#post-17712912)
 * Hi [@sitenerds](https://wordpress.org/support/users/sitenerds/),
 * Could you please share the full code which you have implemented?
 *  I gave a quick test on my test site and it does work out of the box where the“
   Average Speed” field only shows up when the category is added under the following
   line in the code is selected:
 *     ```wp-block-code
         $cat_id = 3;
       ```
   
 * Is there a page URL where you have the form added so we can check and give a 
   closer look?
 * Looking forward to your response.
 * Kind Regards,
 * Nithin

Viewing 15 replies - 1 through 15 (of 22 total)

1 [2](https://wordpress.org/support/topic/conditional-logic-based-on-post-data-field/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/conditional-logic-based-on-post-data-field/page/2/?output_format=md)

The topic ‘Conditional Logic based on post data field’ is closed to new replies.

 * ![](https://ps.w.org/forminator/assets/icon-256x256.gif?rev=3443182)
 * [Forminator Forms – Contact Form, Payment Form & Custom Form Builder](https://wordpress.org/plugins/forminator/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/forminator/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/forminator/)
 * [Active Topics](https://wordpress.org/support/plugin/forminator/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/forminator/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/forminator/reviews/)

## Tags

 * [conditional logic](https://wordpress.org/support/topic-tag/conditional-logic/)
 * [post data](https://wordpress.org/support/topic-tag/post-data/)

 * 22 replies
 * 8 participants
 * Last reply from: [Nithin – WPMU DEV Support](https://wordpress.org/support/users/wpmudevsupport11/)
 * Last activity: [2 years ago](https://wordpress.org/support/topic/conditional-logic-based-on-post-data-field/page/2/#post-17760016)
 * Status: resolved