• Hi all and thanks in advance for any help.

    Let’s say that I have:

    Custom Post Type: Product
    Custom Post Type: Color

    “Color” is defined inside “Product” via an ACF relationship.

    I want to count the total number of products that are red.

    I tried the following without success:

    [calc]total_red = 0[/calc]
    [loop type=product]
      [related color]
        [if field=title value=Red]
           [calc]total_red = total_red + 1[/calc]
       [/if]
     [/related]
    [/loop]
    [get total_red]

    Custom Content Shortcode documentation is amazingly clear. Yet, the IF + RELATED use case is not clear to me.

    Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Try

    
    [loop type=product]
      [related color]
        [if field=title value=Red] for the value you should use 'Red'
           [-if var=total_red]
             [calc]total_red = total_red + 1[/calc]
           [-else]
             [set total_red]1[/set]
           [/-if]
       [/if]
     [/related]
    [/loop]
    [get total_red]
    • This reply was modified 3 years, 7 months ago by polarracing.
    Thread Starter s1s2

    (@s1s2)

    Unfortunately, it’s not working. Forget the calculation for a minute.
    The problem is with the IF statement. When I write:

    [loop type=product]
      [related color]
        [if field=title value=Red]
           Red!
       [/if]
     [/related]
    [/loop]

    I would expect to see the output “Red!” every time one of the products in the loop has that field. But the loop returns nothing.

    To debug, I tried this code:

    [loop type=product]
      [related color]
        [field title]
     [/related]
    [/loop]

    And the output “Red” appears when appropriate.
    So I am doing a mistake in the syntax of the IF statement.

    I tried value=’Red’, value=”Red”, value=Red, but I can’t make it work…

    Try:

    [if field=title value='Red']

    Thread Starter s1s2

    (@s1s2)

    Thanks, but it produces no output. As I said, I tried all the following:

    [if field=title value=Red]
    [if field=title value='Red']
    [if field=title value="Red"]

    No difference in any case.

    Thread Starter s1s2

    (@s1s2)

    I managed to make it work by setting an intermediate variable:

    [loop type=product]
      [related color]
        [set temp][field title][/set]
        [if var=temp value=Red]
           Red!
       [/if]
     [/related]
    [/loop]

    So it seems that the IF statement doesn’t work as expected when comparing against the stated VALUE if the FIELD is a RELATED type.

    In other situations, the IF statement works as expected with the RELATED FIELD type.

    I don’t know if it’s by design or a bug.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to combine IF and RELATED?’ is closed to new replies.