Support

Account

Forum Replies Created

  • Hi,

    I found my answer :

    • field values must be meta queried one at a time (in the query definition), because you should try any order, and it could be mess with as less as 3 values
    • The basic query is on this model, section 3 (Multiple custom field values), since the field queried is a post, with potential multiple values
    • The relation is a “AND” in order to have the meta field like value1 and like value 2 and llike value3
    $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.

    1. Am i correct ?
    2. Is there a way to use a post_object, and let only the ones selected in the option field “bubble up” ?

    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 :

    • i created one more field (let’s call it searchable)
    • you can also create a blacklist to ignore some fields
    • this field is populated on post_save : you get all the fields you want, clean it, and then use update_field
    • you can then use a single meta field in your search query (which is more or less the same one as you)

    Pros :

    • simple searchable meta, usable whatever the structure of the post type is
    • simple sql query
    • customizable for any post type via a blacklist loated in one place for any post type

    Cons :

    • clearly duplicated databse content, but we’re talking about sanitized text, and i really think that’s worth the price
    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

Viewing 6 posts - 1 through 6 (of 6 total)