# Conditional logic if field value = another field value, show field \[RESOLVED\]

**URL:** <https://community.gravityforms.com/t/conditional-logic-if-field-value-another-field-value-show-field-resolved/833>\
**Category:** Get Help\
**Tags:** custom\
**Created:** [January 19, 2019, 6:31pm UTC](https://community.gravityforms.com/t/conditional-logic-if-field-value-another-field-value-show-field-resolved/833 "2019-01-19T18:31:13Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![user5ac85239f1cac6.4](https://avatars.discourse-cdn.com/v4/letter/u/8797f3/32.png) [@user5ac85239f1cac6.4](https://community.gravityforms.com/u/user5ac85239f1cac6.4)\
**Post date:** [January 19, 2019, 6:31pm UTC](https://community.gravityforms.com/t/conditional-logic-if-field-value-another-field-value-show-field-resolved/833/1 "2019-01-19T18:31:13Z")

</div>

My goal is to give a warning when the state does not match another field or alternatively it does not match a page custom field.

I’ve tried a couple different ways but I’m confused about some things.

With this example,

```
add_filter( 'gform_pre_render', 'set_conditional_logic' );
add_filter( 'gform_pre_process', 'set_conditional_logic' );
function set_conditional_logic( $form ) {

    //Set conditional logic only for form 14
    if ( $form['id'] !== 14 ) {
        return $form;
    }
 
    foreach ( $form['fields'] as &$field ) {
        if ( $field->id == 2 ) {
            $field->conditionalLogic =
                array(
                    'actionType' => 'show',
                    'logicType' => 'all',
                    'rules' =>
                        array( array( 'fieldId' => 1, 'operator' => 'is', 'value' => 'First Choice' ) )
                );
        }
    }
    return $form;
}

```

I have trouble with the ‘value’ =\> ‘First Choice’))

I can’t seem to get it to find the value from another field. Alternatively I could compare the value against a custom field in the page meta but I can’t get it to find the page id that the form is currently on. I think because it may be outside the loop?

So I tried gform\_is\_value\_match

My problem here is I don’t see where the comparison field id goes in this javascript example,

```
`gform.addFilter(` `'gform_is_value_match'` `, ` `function` `(isMatch, formId, rule) {`

` ` `// do stuff`

 

` ` `return` `isMatch;`

`} );`

```

And finally, in the PHP example, it includes a comparison of conditional logic views. I can’t quite figure out how to do just a comparison of values in this example.

```
`add_filter( ` `'gform_is_value_match'` `, ` `function` `( ` `$is_match` `, ` `$field_value` `, ` `$target_value` `, ` `$operation` `, ` `$source_field` `, ` `$rule` `) {`

` ` `// do stuff`

 

` ` `return` `$is_match` `;`

`}, 10, 6 );`

```

I may not need to do the PHP part. Any help is appreciated. If it’s too much I’ll just keep trying to figure it out. Thank you.

---

<div class="post-metadata">

**Author:** ![27concepts](https://avatars.discourse-cdn.com/v4/letter/2/edb3f5/32.png) [@27concepts](https://community.gravityforms.com/u/27concepts)\
**Post date:** [January 21, 2019, 4:09pm UTC](https://community.gravityforms.com/t/conditional-logic-if-field-value-another-field-value-show-field-resolved/833/2 "2019-01-21T16:09:18Z")

</div>

If you need to use a post custom field value, you could simply have a hidden field in the form that is dynamically populated using a custom field merge tag: [https://docs.gravityforms.com/merge-tags/#custom-fields](https://docs.gravityforms.com/merge-tags/#custom-fields)

You can also use a merge tag to get the value of the post/page ID the form is embedded on.

Then, you can use the value of the hidden field to do your comparison for conditional logic.

---

<div class="post-metadata">

**Author:** ![user5ac85239f1cac6.4](https://avatars.discourse-cdn.com/v4/letter/u/8797f3/32.png) [@user5ac85239f1cac6.4](https://community.gravityforms.com/u/user5ac85239f1cac6.4)\
**Post date:** [January 21, 2019, 4:25pm UTC](https://community.gravityforms.com/t/conditional-logic-if-field-value-another-field-value-show-field-resolved/833/3 "2019-01-21T16:25:05Z")

</div>

I have tried using a hidden field. But there is no way to do a conditional logic rule that solves my problem that way. And that is, if one field equals another field, show another field. So let’s say the hidden field says Louisiana. And they pick Pennsylvania, it shows a warning through an html field.

The merge tag doesn’t help me in the php code where I need to pull the post/page id from.

How do I use a hidden field to do the comparison logic. There is no option where one field equals another field that I can see?

---

<div class="post-metadata">

**Author:** ![27concepts](https://avatars.discourse-cdn.com/v4/letter/2/edb3f5/32.png) [@27concepts](https://community.gravityforms.com/u/27concepts)\
**Post date:** [January 21, 2019, 4:42pm UTC](https://community.gravityforms.com/t/conditional-logic-if-field-value-another-field-value-show-field-resolved/833/4 "2019-01-21T16:42:31Z")

</div>

I don’t really get the need for gform.addFilter approach here. Maybe you just need to think about this differently.

Actually, if you’re doing everything via php code, you don’t need a hidden field. In your pre\_render function you can get the ID of the post using get\_the\_ID() and thru that, the value of any custom field:

```
$custom_field = get_post_meta(get_the_ID(),"custom-field", true);

```

Then use $custom field to populate a field and for the conditional logic check.

---

<div class="post-metadata">

**Author:** ![user5ac85239f1cac6.4](https://avatars.discourse-cdn.com/v4/letter/u/8797f3/32.png) [@user5ac85239f1cac6.4](https://community.gravityforms.com/u/user5ac85239f1cac6.4)\
**Post date:** [January 21, 2019, 4:49pm UTC](https://community.gravityforms.com/t/conditional-logic-if-field-value-another-field-value-show-field-resolved/833/5 "2019-01-21T16:49:16Z")

</div>

Quite possible that I do need to think of a different approach but I haven’t found one that works without going into php. The problem with the get\_the\_ID() function is that it is called outside the loop where it does not have the id yet. When I attempt to run the code later by adding an action, I get errors.

edit: also, using the global variable doesn’t work either.

---

<div class="post-metadata">

**Author:** ![27concepts](https://avatars.discourse-cdn.com/v4/letter/2/edb3f5/32.png) [@27concepts](https://community.gravityforms.com/u/27concepts)\
**Post date:** [January 21, 2019, 5:04pm UTC](https://community.gravityforms.com/t/conditional-logic-if-field-value-another-field-value-show-field-resolved/833/6 "2019-01-21T17:04:57Z")

</div>

It’s fine to use PHP for this.

Your pre\_render function is able to use get\_the\_ID(). Why do you need to run a separate action function?

---

<div class="post-metadata">

**Author:** ![user5ac85239f1cac6.4](https://avatars.discourse-cdn.com/v4/letter/u/8797f3/32.png) [@user5ac85239f1cac6.4](https://community.gravityforms.com/u/user5ac85239f1cac6.4)\
**Post date:** [January 21, 2019, 5:07pm UTC](https://community.gravityforms.com/t/conditional-logic-if-field-value-another-field-value-show-field-resolved/833/7 "2019-01-21T17:07:39Z")

</div>

It seems to come out 0 for me. Maybe I’m doing something else wrong. I ran the action to run the code at a point where it does have the ID. I read it somewhere.

---

<div class="post-metadata">

**Author:** ![27concepts](https://avatars.discourse-cdn.com/v4/letter/2/edb3f5/32.png) [@27concepts](https://community.gravityforms.com/u/27concepts)\
**Post date:** [January 21, 2019, 5:09pm UTC](https://community.gravityforms.com/t/conditional-logic-if-field-value-another-field-value-show-field-resolved/833/8 "2019-01-21T17:09:26Z")

</div>

What does this action function look like and what is its purpose?

---

<div class="post-metadata">

**Author:** ![user5ac85239f1cac6.4](https://avatars.discourse-cdn.com/v4/letter/u/8797f3/32.png) [@user5ac85239f1cac6.4](https://community.gravityforms.com/u/user5ac85239f1cac6.4)\
**Post date:** [January 21, 2019, 5:14pm UTC](https://community.gravityforms.com/t/conditional-logic-if-field-value-another-field-value-show-field-resolved/833/9 "2019-01-21T17:14:38Z")

</div>

Here’s the example I followed

" Just put your code in some action, that is run later."

```auto
function my_wp_action_callback() {
    if ( is_home() ) {
        add_action( 'shoestrap_below_top_navbar', 'my_custom_below_top_navbar' );
    }
}
add_action( 'wp', 'my_wp_action_callback' );

```

So it runs the code later? It gave a bunch of errors that way. Oh I didn’t put the is\_home() part btw.

---

<div class="post-metadata">

**Author:** ![27concepts](https://avatars.discourse-cdn.com/v4/letter/2/edb3f5/32.png) [@27concepts](https://community.gravityforms.com/u/27concepts)\
**Post date:** [January 21, 2019, 5:15pm UTC](https://community.gravityforms.com/t/conditional-logic-if-field-value-another-field-value-show-field-resolved/833/10 "2019-01-21T17:15:41Z")

</div>

I feel like that is a very twisted approach… what is the purpose of the action function? What is it supposed to do?

---

<div class="post-metadata">

**Author:** ![user5ac85239f1cac6.4](https://avatars.discourse-cdn.com/v4/letter/u/8797f3/32.png) [@user5ac85239f1cac6.4](https://community.gravityforms.com/u/user5ac85239f1cac6.4)\
**Post date:** [January 21, 2019, 5:16pm UTC](https://community.gravityforms.com/t/conditional-logic-if-field-value-another-field-value-show-field-resolved/833/11 "2019-01-21T17:16:44Z")

</div>

Run the code later when the ID is available. I don’t know, I’m just trying anything I can think of.

---

<div class="post-metadata">

**Author:** ![27concepts](https://avatars.discourse-cdn.com/v4/letter/2/edb3f5/32.png) [@27concepts](https://community.gravityforms.com/u/27concepts)\
**Post date:** [January 21, 2019, 5:22pm UTC](https://community.gravityforms.com/t/conditional-logic-if-field-value-another-field-value-show-field-resolved/833/12 "2019-01-21T17:22:26Z")

</div>

I am trying to understand what the code you’re trying to run later is supposed to do. What does that code that runs later look like?

Can it not be placed in your pre\_render function?

---

<div class="post-metadata">

**Author:** ![user5ac85239f1cac6.4](https://avatars.discourse-cdn.com/v4/letter/u/8797f3/32.png) [@user5ac85239f1cac6.4](https://community.gravityforms.com/u/user5ac85239f1cac6.4)\
**Post date:** [January 21, 2019, 5:25pm UTC](https://community.gravityforms.com/t/conditional-logic-if-field-value-another-field-value-show-field-resolved/833/13 "2019-01-21T17:25:25Z")

</div>

I did it because it seemed to not be getting the ID inside the render function. It was coming up 0. So I was trying to find a way to run the code at a later time when the ID is available. I don’t think it was the right approach.

---

<div class="post-metadata">

**Author:** ![user5ac85239f1cac6.4](https://avatars.discourse-cdn.com/v4/letter/u/8797f3/32.png) [@user5ac85239f1cac6.4](https://community.gravityforms.com/u/user5ac85239f1cac6.4)\
**Post date:** [January 21, 2019, 5:34pm UTC](https://community.gravityforms.com/t/conditional-logic-if-field-value-another-field-value-show-field-resolved/833/14 "2019-01-21T17:34:42Z")

</div>

If it helps, here’s what my last attempt looks like,

```
add_filter( 'gform_pre_render', 'set_conditional_logic' );
add_filter( 'gform_pre_process', 'set_conditional_logic' );

function set_conditional_logic( $form ) {
    //Set conditional logic only for form 5
    if ( $form['id'] !== 5 ) {
        return $form;
    }
    if ( $field->id == 167 ) {
			 $custom_field = get_post_meta(get_the_ID(),"state", true);
            $field->conditionalLogic =
                array(
                    'actionType' => 'hide',
                    'logicType' => 'all',
                    'rules' =>
                        array( array( 'fieldId' => 167, 'operator' => 'is', 'value' => $custom_field ) )
                );
        }
    return $form;
  
}

```

Also, thanks for helping.

---

<div class="post-metadata">

**Author:** ![Parkbum](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/parkbum/32/810_2.png) [@Parkbum](https://community.gravityforms.com/u/Parkbum)\
**Post date:** [January 21, 2019, 8:39pm UTC](https://community.gravityforms.com/t/conditional-logic-if-field-value-another-field-value-show-field-resolved/833/15 "2019-01-21T20:39:53Z")

</div>

I could be wrong, but… the problem with this is it is PHP, and builds on loading unless there is something firing it. You need to use jQuery for this to do what you want, either pure jQuery or a jQuery that calls an AJAX function.

I use [Scripts n Styles](https://goadrifttravel.com/wp-admin/plugin-install.php?tab=plugin-information&plugin=scripts-n-styles&TB_iframe=true&width=772&height=496) to put jQuery in my header/footer and it works great.

---

<div class="post-metadata">

**Author:** ![user5ac85239f1cac6.4](https://avatars.discourse-cdn.com/v4/letter/u/8797f3/32.png) [@user5ac85239f1cac6.4](https://community.gravityforms.com/u/user5ac85239f1cac6.4)\
**Post date:** [January 21, 2019, 10:06pm UTC](https://community.gravityforms.com/t/conditional-logic-if-field-value-another-field-value-show-field-resolved/833/16 "2019-01-21T22:06:05Z")

</div>

Oh ok. Thanks for the help, I’ll give that a shot.

---

<div class="post-metadata">

**Author:** ![user5ac85239f1cac6.4](https://avatars.discourse-cdn.com/v4/letter/u/8797f3/32.png) [@user5ac85239f1cac6.4](https://community.gravityforms.com/u/user5ac85239f1cac6.4)\
**Post date:** [January 22, 2019, 1:09am UTC](https://community.gravityforms.com/t/conditional-logic-if-field-value-another-field-value-show-field-resolved/833/17 "2019-01-22T01:09:06Z")

</div>

That worked. Thanks. I’m a little new to both jQuery and PHP.

---

<div class="post-metadata">

**Author:** ![Parkbum](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/parkbum/32/810_2.png) [@Parkbum](https://community.gravityforms.com/u/Parkbum)\
**Post date:** [January 22, 2019, 6:06am UTC](https://community.gravityforms.com/t/conditional-logic-if-field-value-another-field-value-show-field-resolved/833/18 "2019-01-22T06:06:37Z")

</div>

Awesome! I am too, but jQuery does some cool stuff once you get the hang of it.

---

<div class="post-metadata">

**Author:** ![chrishajer](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/chrishajer/32/88_2.png) [@chrishajer](https://community.gravityforms.com/u/chrishajer)\
**Post date:** [January 23, 2019, 6:31am UTC](https://community.gravityforms.com/t/conditional-logic-if-field-value-another-field-value-show-field-resolved/833/19 "2019-01-23T06:31:36Z")

</div>


