Home › Forums › Backend Issues (wp-admin) › Post Object search by ID or SKU
Hello. I have a question.
I have a “Post Object” field for products.
But I have many of the same product names and I want to search products via SKU in my admin panel. How I can do that?
There isn’t any easy way to alter the search to search by custom fields.
In your case you would use an acf/fields/post_object/query filter instead of acf/fields/relationship/query
I saw those topics, but if to be honest I don’t understand what exactly I need to do, can you help me?
Going to be honest, I don’t generally do things like this, all I can do it repeat the examples.
Basically you add an acf filter for your field
add_filter('acf/fields/post_oject/query/name=YOUR-FIELD_NAME', 'YOUR_FUNCTION_NAME', 10, 3);
function YOUR_FUNCTION_NAME($args, $field, $post_id) {
// inside of this filter you add the filters described
// for searching post meta
// as described here and in other places
// https://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/
// this is done so that the filters will only be added for
// this particular field in the admin
add_filter('posts_join', 'cf_search_join');
add_filter('posts_where', 'cf_search_where');
add_filter('posts_distinct', 'cf_search_distinct');
}
Then you add the other function to search by what you want to search by. You will need to refer to the WC documentation for what field to look at for sku.
Just can you change the error in your code, please
add_filter('acf/fields/post_oject/query/name=YOUR-FIELD_NAME', 'YOUR_FUNCTION_NAME', 10, 3);
to
add_filter('acf/fields/post_object/query/name=YOUR-FIELD_NAME', 'YOUR_FUNCTION_NAME', 10, 3);
post_object
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.