Title: get_data returns wrong options
Last modified: August 22, 2016

---

# get_data returns wrong options

 *  [Magenta Cuda](https://wordpress.org/support/users/magenta-cuda/)
 * (@magenta-cuda)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/get_data-returns-wrong-options/)
 * If you use the same field name in two different custom post types get_data is
   apparently returning the options of the first entry in the wp_mf_custom_fields
   table with a matching field name ignoring the post type.
 * [https://wordpress.org/plugins/magic-fields-2/](https://wordpress.org/plugins/magic-fields-2/)

Viewing 1 replies (of 1 total)

 *  Thread Starter [Magenta Cuda](https://wordpress.org/support/users/magenta-cuda/)
 * (@magenta-cuda)
 * [11 years ago](https://wordpress.org/support/topic/get_data-returns-wrong-options/#post-5681693)
 * I think the following will fix this problem:
 *     ```
       function get_data( $field_name, $group_index=1, $field_index=1, $post_id ){
         global $wpdb;
   
         $field_name = str_replace(" ","_",$field_name);
   
         $sql = sprintf(
           "SELECT m.meta_id,w.meta_value,f.type,f.options,f.description,f.label " .
           "FROM %s m " .
           "JOIN %s w ON m.meta_id = w.meta_id " .
           "JOIN %s f ON m.field_name = f.name " .
           "JOIN %s p ON w.post_id = p.ID " .
           "WHERE m.post_id = %d AND m.field_name = '%s' AND m.group_count = %d AND m.field_count = %d AND f.post_type = p.post_type",
           MF_TABLE_POST_META,
           $wpdb->postmeta,
           MF_TABLE_CUSTOM_FIELDS,
           $wpdb->posts,
           $post_id,
           $field_name,
           $group_index,
           $field_index
         );
   
         $result = $wpdb->get_row($sql,ARRAY_A);
   
         if( empty($result) ) return NULL;
   
         $result['options'] = unserialize($result['options']);
   
         if(is_serialized($result['meta_value'])){
           $result['meta_value'] = unserialize( $result['meta_value'] );
         }
   
         return $result;
       }
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘get_data returns wrong options’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/magic-fields-2.svg)
 * [Magic Fields 2](https://wordpress.org/plugins/magic-fields-2/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/magic-fields-2/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/magic-fields-2/)
 * [Active Topics](https://wordpress.org/support/plugin/magic-fields-2/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/magic-fields-2/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/magic-fields-2/reviews/)

 * 1 reply
 * 1 participant
 * Last reply from: [Magenta Cuda](https://wordpress.org/support/users/magenta-cuda/)
 * Last activity: [11 years ago](https://wordpress.org/support/topic/get_data-returns-wrong-options/#post-5681693)
 * Status: not resolved