Hi,
I found my answer :
$values = explode(",", $values_str);
$query_values = [];
foreach($values as $value) {
$query_values[] = [
'key' => 'produit_categories',
'value' => $value,
'compare' => 'LIKE'
];
}
$args['meta_query'] =
'relation' => 'AND',
$query_values
];
Works like a charm : field 1 get only categories defined in the option field, plus one other option field, field 2 get all categories except those ones.
Thanks a lot, John !
// Uniquement les catégories "de niveau 1" (y compris la mise en avant "nouveautés")
function suremesurev1_produit_categories_niv_1_query($args, $field, $post) {
$choices = get_field("produits_categories_niv_1", "option");
$choices[] = get_field("produits_categorie_mise_en_avant", "option");
$args["post__in"] = $choices;
return $args;
}
add_filter('acf/fields/post_object/query/name=produit_categories_niv_1', 'suremesurev1_produit_categories_niv_1_query', 10, 3);
// Toutes les catégories, sauf celles "de niveau 1", ni la mise en avant "nouveautés"
function suremesurev1_produit_categories_query($args, $field, $post) {
$choices = get_field("produits_categories_niv_1", "option");
$choices[] = get_field("produits_categorie_mise_en_avant", "option");
$args["post__not_in"] = $choices;
return $args;
}
add_filter('acf/fields/post_object/query/name=produit_categories', 'suremesurev1_produit_categories_query', 10, 3);
Ok, i’m gonna test this, and i’ll post the results here.
Thanks a lot for this answer, which was as complete as it was precise (yes, non-native speakers can be verbose 🙂 ).
Ok, so, if i understand correctly, you’re telling me that the field i’m using (the second one, which should refer to the values in the optin field) is not managed as a select, but as a post itself, and the result of that is shown as a select (stop me anywhere i’m telling bulshit).
So, either there is a way to use a post_object, which would need a way to get only the posts selected in the option field, or i should use a select, populate it with ids and titles from those posts, and manage it myself afterwards.
Thanks
@sixtyseven : One big problem with this way is that, if you have numerous fields, the query can becomme reaaaallly complex. So i took it another way :
Pros :
Cons :
function save_searchable_content_meta($post_id, $post, $update) {
$fields = get_fields($post->ID);
if (array_key_exists("searchable_content", $fields)) {
unset($fields["searchable_content"]);
array_unshift($fields, $post->post_title, $post->post_content);
$str = sanitize_text_field(array_to_str($fields));
update_field("searchable_content", $str, $post_id);
}
}
add_action('save_post', 'save_searchable_content_meta', 10, 3);
Hi @sixtyseven,
First of all, thanks a lot for the tip.
Second : does your method work with repeaters ?
Thanks
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.