2

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:

enter image description here

1 Answer 1

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).

4
  • I checked your answer, everything looks good. I have a question regarding the $post_id. I want to retrieve all the posts present in the cpac-episodes. 3v4l.org/DuU85 Oct 30, 2019 at 16:00
  • For now, I have to retrieve all the posts and then we can all play with the order (ASC/DESC) or with dates. Oct 30, 2019 at 16:01
  • There's a lot of "it depends" involved in this because it all depends on what exactly your end result is expected to be. I've updated with one possibility of a generic example that gets all posts of a custom post type and loops through. It then echos out the array results of the function for each post.
    – butlerblog
    Oct 30, 2019 at 16:37
  • Makes sense.The custom fields which I want to get for all posts having post type cpac-episdoes are title_short_en and title_en. At this moment, it is 2 but requirement may change in the future. Oct 30, 2019 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.