admin管理员组文章数量:1278910
I have a post type absf-episodes
copied from the URL /wp-admin/edit.php?post_type=absf-episodes
The custom fields which I have for the post type absf-episodes
are:
broadview_updated, description_en, description_fr, description_short_en, description_short_fr, episode_id, title_short_en, title_short_fr
I have a function in which I want to mention those fields so that I can call the function again and again.
function get_down_latest_videos( ) {
}
Posts having post_type=absf-episodes:
I have a post type absf-episodes
copied from the URL /wp-admin/edit.php?post_type=absf-episodes
The custom fields which I have for the post type absf-episodes
are:
broadview_updated, description_en, description_fr, description_short_en, description_short_fr, episode_id, title_short_en, title_short_fr
I have a function in which I want to mention those fields so that I can call the function again and again.
function get_down_latest_videos( ) {
}
Posts having post_type=absf-episodes:
1 Answer
Reset to default 2Custom fields are saved as "post meta" which is stored in wp_postmeta
.
To retrieve them, you use the get_post_meta()
function as follows:
$value = get_post_meta( $post_id, $meta_key, $single );
$single
will almost always be set to true if you're working string data. Unfortunately, it defaults as false, so if you're retrieving the data as a string, you need to specify this as true:
$value = get_post_meta( $post_id, $meta_key, true );
You didn't really give too much info about the data, but let's assume it's all string data and you want to retrieve it in your function. Here's your function getting each of these and putting it into an array as a result:
function get_down_latest_videos( $post_id ) {
$meta_keys = array( 'broadview_updated', 'description_en', 'description_fr', 'description_short_en', 'description_short_fr', 'episode_id', 'title_short_en', 'title_short_fr' );
foreach ( $meta_keys as $meta_key ) {
$my_video_data[ $meta_key ] = get_post_meta( $post_id, $meta_key, true );
}
return $my_video_data;
}
To use it, if you pass the post ID to get_down_latest_videos( $post_id )
(where $post_id
is the ID of your specific post), you'll get back an array with the values of your custom fields where the array is keyed by the post meta key.
Update:
Now, if you want to loop through all of the 'absf-episodes' posts, you can set up a query like the following:
$query = new WP_Query(array(
'post_type' => 'absf-episodes',
'post_status' => 'publish'
'posts_per_page' => -1, // This gets "ALL" instead of limiting the results
));
while ( $query->have_posts() ) {
$query->the_post();
$post_id = get_the_ID();
// You are now looping through each 'absf-episodes' by $post_id.
// This example just spits out the post meta results for each post:
echo '<pre>';
$post_meta = get_down_latest_videos( $post_id );
print_r( $post_meta );
echo '</pre>';
}
Depending on where you use something like this, you may need to use wp_reset_query()
before or after this loop (or possibly both).
本文标签: theme developmentHow to retrieve custom field types for all posts in WordPressPHP
版权声明:本文标题:theme development - How to retrieve custom field types for all posts in WordPressPHP? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741300520a2371065.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论