Hello everyone,
I am trying to add a filter for the relationship field located within a group.
The group has the name: link_scope_pages
The relationship field inside the group: items
I tried the following ways but none worked:
add_filter('acf/fields/relationship/query/name=link_scope_pages_items', 'my_function', 10, 3);
add_filter('acf/fields/relationship/query/name=link_scope_pages_$_items', 'my_function', 10, 3);
add_filter('acf/fields/relationship/query/name=link_scope_pages_%_items', 'my_function', 10, 3);
How can I access the relationship field within the group?
I paste the function I call (my_function) that I tested.
function my_function( $args, $field, $post_id ) {
$args = [
'post_type' => 'page',
'meta_query' => [
[
'key' => '_wp_page_template',
'value' => 'tpl-scope.php',
]
]
];
return $args;
}
Thanks for support
Hi yes I solved it using the field key within the group.
But I wanted to know how it works via name if it is possible.
To be honest, I don’t know exactly without testing. I do know that the field name is not what you would expect when calls fires the hook. It does have to do with the hierarchy of the field. I would expect your first example to work, but, it the group field also a sub field of some other field? Is it part of a cloned fields?
The only way to find out is to create a filter for all fields and output it to see what the name is.
// all fields filter
add_filter('acf/prepare_field', 'output_field_object');
function output_field_object($field) {
echo '<pre>'; print_r($field); echo '</pre>';
return $field;
}
There have been times when I needed to use the field name and I have had to create my filter for all fields and then use ‘strpos()` to test the field name or even the field key to see if it is the field I need to filter. There is always some logic to it as to why the name is what it is for the filter.
But for the most part, whenever I am dealing with filtering a sub field of any kind I just use the field key.
Hi,
with the filter you provided me the answer is this below.
The group is simple and is not contained in other groups or a clone.
Array
(
[ID] => 0
[key] => field_5f1b04a650dbe
[label] =>
[name] => acf[field_5f1b04a650dbe]
[prefix] => acf
[type] => group
[value] => Array
(
[field_5fad6bdaa2903] => 1
[field_5f1b0571deda4] =>
)
[menu_order] => 0
[instructions] =>
[required] => 0
[id] => acf-field_5f1b04a650dbe
[class] =>
[conditional_logic] => 0
[parent] => group_5f1b04a64d28d
[wrapper] => Array
(
[width] =>
[class] =>
[id] =>
)
[layout] => block
[_name] => link_scope_pages
[_valid] => 1
[sub_fields] => Array
(
[0] => Array
(
[ID] => 0
[key] => field_5fad6bdaa2903
[label] => Automatico
[name] => is_automatic
[prefix] => acf
[type] => true_false
[value] =>
[menu_order] => 0
[instructions] =>
[required] => 0
[id] =>
[class] =>
[conditional_logic] => 0
[parent] => field_5f1b04a650dbe
[wrapper] => Array
(
[width] =>
[class] =>
[id] =>
)
[message] =>
[default_value] => 1
[ui] => 1
[ui_on_text] =>
[ui_off_text] =>
[wpml_cf_preferences] => 0
[_name] => is_automatic
[_valid] => 1
)
[1] => Array
(
[ID] => 0
[key] => field_5f1b0571deda4
[label] => Seleziona pagine ambito
[name] => items
[prefix] => acf
[type] => relationship
[value] =>
[menu_order] => 1
[instructions] =>
[required] => 1
[id] =>
[class] =>
[conditional_logic] => Array
(
[0] => Array
(
[0] => Array
(
[field] => field_5fad6bdaa2903
[operator] => !=
[value] => 1
)
)
)
[parent] => field_5f1b04a650dbe
[wrapper] => Array
(
[width] =>
[class] =>
[id] =>
)
[post_type] => Array
(
[0] => page
)
[taxonomy] =>
[filters] => Array
(
[0] => search
)
[elements] => Array
(
[0] => featured_image
)
[min] =>
[max] =>
[return_format] => object
[wpml_cf_preferences] => 0
[_name] => items
[_valid] => 1
)
)
[_prepare] => 1
)
I don’t know why the group field has that name in this case.
What happens if you change add filter to
add_filter('acf/prepare_field', 'output_field_object', 1);
what name should it have?
Array
(
[ID] => 0
[key] => field_5fad1c053194c
[label] =>
[name] => acf[field_5fad1c053194c]
[prefix] => acf
[type] => group
[value] => Array
(
[field_5fad1f55299f4] =>
[field_5fad1c313194d] => 1
[field_5fad1c493194e] => Array
(
[0] => Array
(
[field_5fad1c733194f] => Emergenze
[field_5fad1d7e31954] => 1
[field_5fad643cea81d] => Array
(
[field_5f9c1fb8b4913] => Array
(
[field_5f9c211cb4914] => 6925
[field_5f9c27fab4915] => Scopri di più
)
)
[field_5fad1cde31950] => Lorem ipsum
[field_5fad1cf131951] => Prestiamo soccorso alle popolazioni vittime di emergenze umanitarie, attraverso la distribuzione di beni primari e la ricostruzione. Oltre l’emergenza, creiamo le condizioni perché l’aiuto si trasformi in un percorso di sviluppo e autonomia per le comunità locali.
[field_5fad1dea31955] => 7004
)
[1] => Array
(
[field_5fad1c733194f] => Sviluppo
[field_5fad1d7e31954] => 0
[field_5fad643cea81d] => Array
(
[field_5f9c1fb8b4913] => Array
(
[field_5f9c211cb4914] =>
[field_5f9c27fab4915] =>
)
)
[field_5fad1cde31950] => Test
[field_5fad1cf131951] => Prestiamo soccorso alle popolazioni vittime di emergenze umanitarie, attraverso la distribuzione di beni primari e la ricostruzione. Oltre l’emergenza, creiamo le condizioni perché l’aiuto si trasformi in un percorso di sviluppo e autonomia per le comunità locali. Prestiamo soccorso alle popolazioni vittime di emergenze umanitarie, attraverso la distribuzione di beni primari e la ricostruzione. Oltre l’emergenza, creiamo le condizioni perché l’aiuto si trasformi in un percorso di sviluppo e autonomia per le comunità locali.
[field_5fad1dea31955] => 7007
)
)
)
[menu_order] => 0
[instructions] =>
[required] => 0
[id] => acf-field_5fad1c053194c
[class] =>
[conditional_logic] => 0
[parent] => group_5fad1bae10663
[wrapper] => Array
(
[width] =>
[class] =>
[id] =>
)
[layout] => block
[wpml_cf_preferences] => 0
[_name] => featured_blocks
[_valid] => 1
[sub_fields] => Array
(
[0] => Array
(
[ID] => 0
[key] => field_5fad1f55299f4
[label] => Informazioni
[name] =>
[prefix] => acf
[type] => message
[value] =>
[menu_order] => 0
[instructions] =>
[required] => 0
[id] =>
[class] =>
[conditional_logic] => 0
[parent] => field_5fad1c053194c
[wrapper] => Array
(
[width] =>
[class] =>
[id] =>
)
[wpml_cf_preferences] => 0
[message] => In evidenza mostra in alto alla pagina subito dopo la testata uno o più blocchi (con metodologia tab) contenenti titolo, descrizione, immagine ed eventuale link se reso visibile della pagina selezionata.
[new_lines] => wpautop
[esc_html] => 0
[_name] =>
[_valid] => 1
)
[1] => Array
(
[ID] => 0
[key] => field_5fad1c313194d
[label] => Attivo
[name] => is_active
[prefix] => acf
[type] => true_false
[value] =>
[menu_order] => 1
[instructions] =>
[required] => 0
[id] =>
[class] =>
[conditional_logic] => 0
[parent] => field_5fad1c053194c
[wrapper] => Array
(
[width] =>
[class] =>
[id] =>
)
[message] =>
[default_value] => 0
[ui] => 1
[ui_on_text] =>
[ui_off_text] =>
[wpml_cf_preferences] => 0
[_name] => is_active
[_valid] => 1
)
[2] => Array
(
[ID] => 0
[key] => field_5fad1c493194e
[label] => Blocchi in evidenza
[name] => items
[prefix] => acf
[type] => repeater
[value] =>
[menu_order] => 2
[instructions] =>
[required] => 1
[id] =>
[class] =>
[conditional_logic] => Array
(
[0] => Array
(
[0] => Array
(
[field] => field_5fad1c313194d
[operator] => ==
[value] => 1
)
)
)
[parent] => field_5fad1c053194c
[wrapper] => Array
(
[width] =>
[class] =>
[id] =>
)
[wpml_cf_preferences] => 0
[collapsed] => field_5fad1c733194f
[min] => 1
[max] => 0
[layout] => block
[button_label] => Aggiungi blocco
[_name] => items
[_valid] => 1
[sub_fields] => Array
(
[0] => Array
(
[ID] => 0
[key] => field_5fad1c733194f
[label] => Etichetta
[name] => label
[prefix] => acf
[type] => text
[value] =>
[menu_order] => 0
[instructions] =>
[required] => 1
[id] =>
[class] =>
[conditional_logic] => 0
[parent] => field_5fad1c493194e
[wrapper] => Array
(
[width] =>
[class] =>
[id] =>
)
[wpml_cf_preferences] => 2
[default_value] =>
[placeholder] =>
[prepend] =>
[append] =>
[maxlength] =>
[_name] => label
[_valid] => 1
)
[1] => Array
(
[ID] => 0
[key] => field_5fad1d7e31954
[label] => Link a pagina
[name] => cta_is_active
[prefix] => acf
[type] => true_false
[value] =>
[menu_order] => 1
[instructions] =>
[required] => 0
[id] =>
[class] =>
[conditional_logic] => 0
[parent] => field_5fad1c493194e
[wrapper] => Array
(
[width] =>
[class] =>
[id] =>
)
[wpml_cf_preferences] => 0
[message] =>
[default_value] => 0
[ui] => 1
[ui_on_text] =>
[ui_off_text] =>
[_name] => cta_is_active
[_valid] => 1
)
[2] => Array
(
[ID] => 0
[key] => field_5fad643cea81d
[label] =>
[name] => link
[prefix] => acf
[type] => clone
[value] =>
[menu_order] => 2
[instructions] =>
[required] => 0
[id] =>
[class] =>
[conditional_logic] => Array
(
[0] => Array
(
[0] => Array
(
[field] => field_5fad1d7e31954
[operator] => ==
[value] => 1
)
)
)
[parent] => field_5fad1c493194e
[wrapper] => Array
(
[width] =>
[class] =>
[id] =>
)
[clone] => Array
(
[0] => group_5f9c1faa3f1a8
)
[display] => group
[layout] => block
[prefix_label] => 1
[prefix_name] => 0
[wpml_cf_preferences] => 0
[_name] => link
[_valid] => 1
[sub_fields] => Array
(
[0] => Array
(
[ID] => 0
[key] => field_5f9c1fb8b4913
[label] =>
[name] => link_to_page
[prefix] => acf
[type] => group
[value] =>
[menu_order] => 0
[instructions] =>
[required] => 0
[id] =>
[class] =>
[conditional_logic] => 0
[parent] => group_5f9c1faa3f1a8
[wrapper] => Array
(
[width] =>
[class] =>
[id] =>
)
[wpml_cf_preferences] => 1
[layout] => block
[_name] => link_to_page
[_valid] => 1
[sub_fields] => Array
(
[0] => Array
(
[ID] => 0
[key] => field_5f9c211cb4914
[label] => Collegamento
[name] => post
[prefix] => acf
[type] => post_object
[value] =>
[menu_order] => 0
[instructions] => Seleziona il collegamento dall'elenco.
[required] => 1
[id] =>
[class] =>
[conditional_logic] => 0
[parent] => field_5f9c1fb8b4913
[wrapper] => Array
(
[width] => 50
[class] =>
[id] =>
)
[wpml_cf_preferences] => 1
[post_type] => Array
(
[0] => page
)
[taxonomy] =>
[allow_null] => 0
[multiple] => 0
[return_format] => object
[ui] => 1
[_name] => post
[_valid] => 1
)
[1] => Array
(
[ID] => 0
[key] => field_5f9c27fab4915
[label] => Etichetta pulsante
[name] => label
[prefix] => acf
[type] => text
[value] =>
[menu_order] => 1
[instructions] => Se il campo non viene valorizzato, viene preso automaticamente il titolo.
[required] => 0
[id] =>
[class] =>
[conditional_logic] => 0
[parent] => field_5f9c1fb8b4913
[wrapper] => Array
(
[width] => 50
[class] =>
[id] =>
)
[wpml_cf_preferences] => 2
[default_value] =>
[placeholder] =>
[prepend] =>
[append] =>
[maxlength] =>
[_name] => label
[_valid] => 1
)
)
[_clone] => field_5fad643cea81d
[__key] => field_5f9c1fb8b4913
[__name] => link_to_page
[__label] =>
)
)
)
[3] => Array
(
[ID] => 0
[key] => field_5fad1cde31950
[label] => Titolo
[name] => title
[prefix] => acf
[type] => text
[value] =>
[menu_order] => 3
[instructions] =>
[required] => 0
[id] =>
[class] =>
[conditional_logic] => 0
[parent] => field_5fad1c493194e
[wrapper] => Array
(
[width] =>
[class] =>
[id] =>
)
[wpml_cf_preferences] => 2
[default_value] =>
[placeholder] =>
[prepend] =>
[append] =>
[maxlength] =>
[_name] => title
[_valid] => 1
)
[4] => Array
(
[ID] => 0
[key] => field_5fad1cf131951
[label] => Descrizione
[name] => description
[prefix] => acf
[type] => textarea
[value] =>
[menu_order] => 4
[instructions] =>
[required] => 1
[id] =>
[class] =>
[conditional_logic] => 0
[parent] => field_5fad1c493194e
[wrapper] => Array
(
[width] =>
[class] =>
[id] =>
)
[default_value] =>
[placeholder] =>
[maxlength] =>
[rows] => 3
[new_lines] =>
[wpml_cf_preferences] => 2
[_name] => description
[_valid] => 1
)
[5] => Array
(
[ID] => 0
[key] => field_5fad1dea31955
[label] => Immagine
[name] => image_desktop
[prefix] => acf
[type] => image_aspect_ratio_crop
[value] =>
[menu_order] => 5
[instructions] =>
[required] => 1
[id] =>
[class] =>
[conditional_logic] => 0
[parent] => field_5fad1c493194e
[wrapper] => Array
(
[width] =>
[class] =>
[id] =>
)
[wpml_cf_preferences] => 0
[crop_type] => aspect_ratio
[aspect_ratio_width] => 567
[aspect_ratio_height] => 401
[return_format] => array
[preview_size] => full
[library] => all
[min_width] =>
[min_height] =>
[min_size] =>
[max_width] =>
[max_height] =>
[max_size] =>
[mime_types] =>
[_name] => image_desktop
[_valid] => 1
)
)
)
)
[_prepare] => 1
)
This is my filter that works
function filter_relationship_acf_scope_pages( $args, $field, $post_id ) {
$args = [
'post_type' => 'page',
'meta_query' => [
[
'key' => '_wp_page_template',
'value' => 'tpl-scope.php',
]
]
];
return $args;
}
add_filter('acf/fields/relationship/query/key=field_5f1b0571deda4', 'filter_relationship_acf_scope_pages', 10, 3);
I’m not sure what is causing your issue. I just set up a test field group containing a group field with a relationship field as a sub field. The name of the relationship field is simply “relationship”. I then added an acf/fields/relationship/query/name=relationship and this filter runs as expected.
Hi, my filter works if I put the name of the relationship field.
My problem is that I have many group fields with relationship fields inside all with the name “items” so the filter applies to all these fields.
I want to apply the filter only to the group field with the name “link_scope_pages” and in particular to its relation subfield with the name “items”.
From various topics such as wp_query I tried to use the group name “_” name of the subfield relationship “link_scope_pages_items” but this doesn’t work.
Here is my problem.
At the moment I solved it with the key name of the relationship subfield (it seems to be unique), but I wanted to know if there was a solution with the names or even putting conditions inside the filter.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.