{"action":"created","issue":{"url":"https://api.github.com/repos/pods-framework/pods/issues/3000","labels_url":"https://api.github.com/repos/pods-framework/pods/issues/3000/labels{/name}","comments_url":"https://api.github.com/repos/pods-framework/pods/issues/3000/comments","events_url":"https://api.github.com/repos/pods-framework/pods/issues/3000/events","html_url":"https://github.com/pods-framework/pods/issues/3000","id":83238740,"number":3000,"title":"Metadata (post field) is not flushed after updating (2.4.3, 2.5.2)","user":{"login":"tuanmh","id":3193353,"avatar_url":"https://avatars.githubusercontent.com/u/3193353?v=3","gravatar_id":"","url":"https://api.github.com/users/tuanmh","html_url":"https://github.com/tuanmh","followers_url":"https://api.github.com/users/tuanmh/followers","following_url":"https://api.github.com/users/tuanmh/following{/other_user}","gists_url":"https://api.github.com/users/tuanmh/gists{/gist_id}","starred_url":"https://api.github.com/users/tuanmh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tuanmh/subscriptions","organizations_url":"https://api.github.com/users/tuanmh/orgs","repos_url":"https://api.github.com/users/tuanmh/repos","events_url":"https://api.github.com/users/tuanmh/events{/privacy}","received_events_url":"https://api.github.com/users/tuanmh/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-06-01T00:28:02Z","updated_at":"2015-06-01T00:35:29Z","closed_at":null,"body":"I've tested with 2 versions: 2.4.3 and 2.5.2\r\nWhenever you read a meta_data field (1) (get_post_meta) and then update it (update_post_meta) (2). The value in (1) will be stored in cache and we will never have the newly updated value. However, if you do an update first and then get it, it will work as expected. \r\n\r\nSteps to reproduce:\r\n- Create a post field called ```bob``` within a post type\r\n- Create a post under the post type\r\n- Execute this script using the post id (replace $postId val in the script) created in the previous step, you would see two identical values printed in screen. \r\n```php\r\n<?php\r\nglobal $wp_did_header;\r\n\r\nif (isset($wp_did_header)) {\r\n    throw new Exception(\"Function called from the wrong place.\");\r\n}\r\n\r\n// Load wordpress\r\n$wp_did_header = true;\r\nrequire_once dirname(__FILE__) . '/wp-load.php';\r\nwp();\r\n\r\n$postId = 61117;\r\n\r\n$bob = get_post_meta($postId, 'bob', true);\r\necho \"$bob <br>\";\r\n\r\nupdate_post_meta($postId, 'bob', md5(time()) . \" Update 1\");\r\n\r\nsleep(1);\r\n$bob = get_post_meta($postId, 'bob', true);\r\necho \"$bob <br>\";\r\n```\r\n- Repeat previous N number of times, old cached value appears instead of new one\r\n- Execute the following script N times, it will work as expected (at least, different values at different refreshes):\r\n```php\r\n<?php\r\nglobal $wp_did_header;\r\n\r\nif (isset($wp_did_header)) {\r\n    throw new Exception(\"Function called from the wrong place.\");\r\n}\r\n\r\n// Load wordpress\r\n$wp_did_header = true;\r\nrequire_once dirname(__FILE__) . '/wp-load.php';\r\nwp();\r\n\r\n$postId = 61117; //replace your postId here\r\n\r\nupdate_post_meta($postId, 'bob', md5(time()) . \" Update 1\");\r\n\r\n$bob = get_post_meta($postId, 'bob', true);\r\necho \"$bob <br>\";\r\n\r\n$bob = get_post_meta($postId, 'bob', true);\r\necho \"$bob <br>\";\r\n```\r\n\r\nThis is causing particular 2 problems (which are highly related):\r\n- After an update following by a read, in the same request, if someone retrieves meta_data again in the same HTTP request, the returned value even won't be the newly updated value\r\n- Any subsequent reads in subsequent HTTP requests will return old value. \r\n\r\nHere is the timeline when executing update_post_meta. \r\n```update_post_meta --> PodsMeta::update_meta --> Pods::save --> Pods::fetch```\r\nI believe the root cause is in Pods::fetch function. This function tries to get the latest values of a post from DB and then does a merge with current data with the same instance. However, it does not get the latest field values, as a result, merging keeps old values and they remain in cache. \r\n\r\n```php\r\n$find = get_posts( $args );\r\nif ( !empty( $find ) )\r\n   $this->row = get_object_vars( $find[ 0 ] );\r\n```\r\n```php\r\nif ( !$explicit_set && !empty( $this->row ) && is_array( $this->row ) && !empty( $old_row ) ) {\r\n   $this->row = array_merge( $old_row, $this->row );\r\n}\r\n```\r\n\r\nAnd perhaps, there are two ways to solve this.\r\n1. Do a proper merge in Pods::fetch. This will include all fields from db, it's quite redundant and is a waste\r\n2. in PodsMeta:update_meta, set the field, e.g. ```bob``` to new value before update. This will make sure everything gets merged and cached correctly. Only works with modified field so won't affect anyone else and optimised. \r\n\r\nThank you for being patient to read my post :) \r\n"},"comment":{"url":"https://api.github.com/repos/pods-framework/pods/issues/comments/107269836","html_url":"https://github.com/pods-framework/pods/issues/3000#issuecomment-107269836","issue_url":"https://api.github.com/repos/pods-framework/pods/issues/3000","id":107269836,"user":{"login":"tuanmh","id":3193353,"avatar_url":"https://avatars.githubusercontent.com/u/3193353?v=3","gravatar_id":"","url":"https://api.github.com/users/tuanmh","html_url":"https://github.com/tuanmh","followers_url":"https://api.github.com/users/tuanmh/followers","following_url":"https://api.github.com/users/tuanmh/following{/other_user}","gists_url":"https://api.github.com/users/tuanmh/gists{/gist_id}","starred_url":"https://api.github.com/users/tuanmh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tuanmh/subscriptions","organizations_url":"https://api.github.com/users/tuanmh/orgs","repos_url":"https://api.github.com/users/tuanmh/repos","events_url":"https://api.github.com/users/tuanmh/events{/privacy}","received_events_url":"https://api.github.com/users/tuanmh/received_events","type":"User","site_admin":false},"created_at":"2015-06-01T00:35:29Z","updated_at":"2015-06-01T00:35:29Z","body":"Something similar to this patch https://github.com/tuanmh/pods/commit/14ddff267040961853e122e6e18a9e58eccba55c would help."}}