[wp-trac] [WordPress Trac] #43949: Need filter in get_metadata

WordPress Trac noreply at wordpress.org
Fri May 4 15:21:52 UTC 2018


#43949: Need filter in get_metadata
-----------------------------------------+------------------------------
 Reporter:  fitehal                      |       Owner:  (none)
     Type:  enhancement                  |      Status:  new
 Priority:  normal                       |   Milestone:  Awaiting Review
Component:  Plugins                      |     Version:
 Severity:  normal                       |  Resolution:
 Keywords:  has-patch reporter-feedback  |     Focuses:
-----------------------------------------+------------------------------

Comment (by fitehal):

 Hi @johnbillion,

 Currently, there is no way to manipulate the metadata dynamically. If we
 are creating a theme or plugin it is not required. We can manipulate the
 data after I have fetched it from the database. But when we are extending
 or building upon some existing plugin which stores data in meta table not
 being able to add or remove some fields based on some login is not
 possible.

 Existing get_{$meta_type}_metadata filter works before fetching data. If
 we want to do some data manipulation based on the value stored in the
 database it is not possible or it is difficult. There is no simple way or
 what we can call WordPress way to achieve it.

 Another issue is that get_{$meta_type}_metadata filter will work only if
 the key is specified. If the user calls get_post_meta($post_id) without
 passing the key, the above filter is not called and is not of any use.

 We were trying to extend a plugin which stores data in wp_postmeta table.
 We have added about 30 custom fields through the plugin on 1 page. We
 wanted those fields to with some default values set to get loaded on all
 pages. Doing it manually would take a lot of time and if we add fields
 directly to the database they will be available only to existing posts and
 not any new post we create.

 While trying to solve this problem first thing that comes to our mind was
 there must be some kind of hook on get_post_meta function and we can use
 it to push our custom fields to an array which is returned from the
 database that way we can have the fields with default value without having
 to insert them in database. Those fields will be saved in the database
 automatically if the user saves the post.

 Looking forward to knowing your thoughts on it.

 @samirmalpande Thanks for the Patch!

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/43949#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list