Support

Account

Home Forums ACF PRO Post Object Filter by Custom Field, not title (ACF PRO)

Helping

Post Object Filter by Custom Field, not title (ACF PRO)

  • Hi there,

    So I am using post object which allows me to search a custom post type by the title. What I want to do is search the custom post type by a custom field and have the custom field values returned. At the moment I have the following code in my functions.php file:-

    function my_post_object_query( $args, $field, $post_id ) {
    	
        // Get the search text
        $the_search = $args['s'];
        
        // Remove it so ACF won't search the posts based on title
        unset($args['s']);
        
        // Search based on custom field
        $args['meta_key'] = 'short_title';
        $args['meta_value'] =  $the_search;
        $args['meta_compare'] = 'LIKE';
        
    	// return
        return $args;
        
    }
    
    // filter for a specific field based on it's name
    add_filter('acf/fields/post_object/query/name=product', 'my_post_object_query', 10, 3);

    This is allowing me to search using a custom field but the returned values are still the ‘Title’. How can I change the returned values to the same custom field; short_title

    Thanks

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

The topic ‘Post Object Filter by Custom Field, not title (ACF PRO)’ is closed to new replies.