When you are editing content that has Post Fields, some fields can be displayed conditionally. You can implement such dependencies using the conditional display feature in Types Post Fields.
For example, imagine you are editing “books”. The “book PDF” field should appear only if the “book available” checkbox is selected. When a book is not available, there is no point in asking for its PDF file.
There are two ways to create such a filter:
- simple display logic
- customized display logic
Simple conditional fields logic
Please note that there are few types of post fields you cannot use as conditions for showing other fields. These fields include the Embedded Media, File Fields (Audio, Video, Image and File Upload field), Multiple Checkboxes field, and a WYSIWYG Text Area field. Additionally, repeating fields also cannot be used as conditions for showing other fields.
To add a simple logical condition for a Post Field Group, click on Edit in the Settings for section and navigate to Data-dependant tab.
For a specific post field in the group, click on Set condition(s) of the Conditional display section, in order to open the conditional display dialog box.
Click Add Condition and set the desired filter.
Conditional fields based on expressions
In order to have more customization power in the filter’s logic, click on Advanced text mode at the conditions dialog box.
In the Customized conditions text box, you can use any of the following evaluation expressions:
- Values from post fields, starting with a dollar sign ($)
- Integer and floating-point numbers
- Strings wrapped inside single quotation marks
- Math operators: +, -, *, /
- Comparison operators: <, >, =, <=, >=, !=
- Boolean operators: AND, OR, NOT
- Nested expressions in several levels of parentheses
- empty() function that checks for blank or non-existing fields
Post field values
You can obtain the value of a post field, prefixed with a dollar sign, and use it in the evaluated expression. For example, to set a display condition when the rating post Field is equal to 4, you should enter the following:
$(rating) = 4
Integer and floating-point numbers
Similar to the previous example, you can also use integer and floating-point numbers in your expressions:
$(rating) = 4.5
If your post field contains text, you can use a string wrapped inside single quotation marks, when checking its value:
$(summary) = ‘This is a summary’
You can enrich your expressions with math operators +, -, *, /
$(ratings-summary)/$(total-ratings) = 5
Apart from the equality operator from the previous examples, you can also use the comparison operators <, >, <=, >=, !=
$(ratings-summary)/$(total-ratings) >= 5
Using the boolean operators AND, OR, NOT, you can combine more expressions to obtain your evaluation:
( $(rating) > 5 ) AND ( $(total-ratings) > 100 )
You can have several levels of parentheses, in order to build nested expressions:
( ( $(rating) > 5 ) AND ( $(total-ratings) > 100 ) OR ( $(total-ratings) = 0 ) )
To check for blank or non-existing fields, you can use the empty() function: