28th Jan '24
/
0 comments

Conditionally Render ACF Custom Fields in the Post Editor

In the BricksLabs Facebook group a user asked:

Hello! ???? Is it possibile to conditionally show ACF meta boxes based on how many parents a custom post type has? For example, show only if the post being edited has 2 parents.

I attempted to use the filter acf/prepare_field, but without success.

Is there a tutorial or example code available that can guide me in achieving this conditional display? Thanks in advance! ????

ACF has a handy acf/prepare_field filter using which custom fields can be conditionally output when editing posts (can be of any post type).

To render a ACF custom field named “location_specialty” only if the current (location) post has exactly two parents using the acf/prepare_field/name=example_field filter, add this code in

Add the following in child theme‘s functions.php (w/o the opening PHP tag) or a code snippets plugin:

<?php 

add_filter( 'acf/prepare_field/name=location_specialty', function ( $field ) {
	return count( get_post_ancestors( get_the_ID() ) ) === 2 ? $field : false;
} );

Use case: Let’s say you have these location posts:

and you want a “Location Specialty” custom field that’s in a field group attached to the “Location” CPT to only appear when Melbourne (or any other location that has 2 parents similarly) is being edited. This code snippet does the job.

Reference

https://developer.wordpress.org/reference/functions/get_post_ancestors/

Get access to all 633 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Pro
ACF Checkbox Sub Field as Unordered List in Bricks

ACF Checkbox Sub Field as Unordered List in Bricks

Updated on 22 Sep 2023 This Pro tutorial shows how the checked options of a Checkbox-type of Sub Field inside a ACF Repeater field can…
Categories:
Pro
Bidirectional Relationship between a CPT and a Taxonomy of another CPT using ACF in Bricks

Bidirectional Relationship between a CPT and a Taxonomy of another CPT using ACF in Bricks

A couple of members asked: I have a cpt called "Markets" aand a cpt "tools". Tools have a taxonomy "tools group". How can i create…
Custom Function for ACF Link Field

Custom Function for ACF Link Field

How to output the label of an ACF link field through Dynamic Data.
Categories:
Tags:
How to Remove Name and Website Fields in Bricks Comment Forms

How to Remove Name and Website Fields in Bricks Comment Forms

Updated on 14 Mar 2024 In the BricksLabs Facebook group a user asked: What is the best method to remove fields from the Bricks "Comments"…
Pro
ACF Flexible Content Field – Layout Labels and Sub Field Labels

ACF Flexible Content Field – Layout Labels and Sub Field Labels

How to output the text of layout labels and/or sub field labels using the Flexible Content field of ACF Pro.
Categories:
Pro
ACF Date Field Value Custom Format in Bricks

ACF Date Field Value Custom Format in Bricks

In the Bricks forum, a user asks: Hi there, I am setting up an events calendar using bricks. In some circumstances I would like to…
Categories:
Tags: