Hello,
I have set up a Post Object field, where i can search for ‘products’ post type.
Each ‘products’ has 2 custom fields, called ‘sku_number’ and ‘old_id’
I am trying to modify the Post Object filter, so that i can searc by title, or sku_numer or old_id
So far i managed to change the filter, but it only works with one custom field, and it removes the search by title.
Can you please help me?
Thank you!
function filter_products_by_sku( $args, $field, $post_id ) {
$the_search = $args['s'];
unset($args['s']);
$args['meta_key'] = 'sku_number';
$args['meta_value'] = $the_search;
$args['meta_compare'] = 'LIKE';
return $args;
}
add_filter('acf/fields/post_object/query/key=field_5f58a23b95d62', 'filter_products_by_sku', 10, 3);
Update
I managed to make it filter based on 2 custom fields, but it is not searching by title now
function filter_products_by_sku( $args, $field, $post_id ) {
$the_search = $args['s'];
unset($args['s']);
$args['meta_query'] = array(
'relation' => 'OR',
array(
'key' => 'sku_number',
'value' => $the_search,
'compare' => 'LIKE',
),
array(
'key' => 'old_id',
'value' => $the_search,
'compare' => 'LIKE',
)
);
return $args;
}
add_filter('acf/fields/post_object/query/key=field_5f58a23b95d62', 'filter_products_by_sku', 10, 3);
You need to modify the where portion of the query. This means that in your filter above you need to add additional filters as described in this post https://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/