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:

Share Improve this question edited Oct 30, 2019 at 16:19 user5447339 asked Oct 30, 2019 at 15:02 user5447339user5447339 819 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Custom 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