[wp-trac] [WordPress Trac] #30058: Defined meta fields with expected data types for post type objects
WordPress Trac
noreply at wordpress.org
Tue Oct 21 12:32:02 UTC 2014
#30058: Defined meta fields with expected data types for post type objects
-------------------------------+-----------------------------
Reporter: ajf- | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Posts, Post Types | Version: trunk
Severity: normal | Keywords:
Focuses: |
-------------------------------+-----------------------------
There is currently no way of programmatically knowing which meta fields
the theme or plugin author is adding to custom post types. Custom fields
and meta boxes have their API, but the implementation details are left to
the author, and there's no way of querying what fields a post type
expects, which of them are required, and what data type each of them uses.
For example, if a plugin or theme author registers a post type for
'''weather forecast:''' which has a ''temperature'' post meta key and a
meta box that allows to edit that value.
{{{
#!php
register_post_type('weather_forecast');
// Add a meta box for 'temperature'
add_action( 'add_meta_boxes', function() { /* ... */ });
// On save post, save the meta data
add_action( 'save_post', function() { /* ... */ });
}}}
A use case for this is in the context of testing and automatic post
generators for creating sample data. If the post meta model was defined in
advance (in `register_post_type`, for example), plugins and theme authors
would be able to know in advance what fields that post type expects.
Here is an example of this in the `register_post_type` function:
{{{
#!php
$args = array(
'meta' => array(
'temperature' => array(
'data_type' => 'INT', // Could also be 'string', or even
strings like 'price'.
'required' => true
);
);
);
}}}
In the context of testing or automatic sample data generation, one could
then do something of the sorts of:
{{{
#!php
$available_post_types = get_post_types(array(
'public' => true
), 'objects');
foreach ($available_post_types as $post_type) {
$generated_id = wp_insert_post(array(/* .. */));
foreach ($post_type->meta as $expected_meta_key =>
$expected_meta_value) {
$generated_meta_value =
generate_random_value_for($expected_meta_value); // fictitious function
add_post_meta($generated_id, $expected_meta_key,
$generated_meta_value);
}
}
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/30058>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list