0

I have register bellow custom post type in function.php and I am using Advanced Custom Fields. Using advance custom field type I have added one image field and assigned to my Release custom post type.

Now I have added one post inside Release post type. I have fill this details in this post.

Title: Speak Now (Deluxe Edition)
Image: Added some Image

Now I want to retrieve meta data with specific post by title/id.

I have tried this code but not working:

$my_post_meta = get_post_meta(556, 'album_image', true);
    if ( ! empty ( $my_post_meta ) ) {
        get_field('album_image');
    }

My custom post type:

/**
 * Music/Releases
 *
 */
function codex_custom_init5() {
  $labels = array(
    'name'               => 'Releases',
    'singular_name'      => 'Release',
    'add_new'            => 'Add New',
    'add_new_item'       => 'Add New Release',
    'edit_item'          => 'Edit Release',
    'new_item'           => 'New Release',
    'all_items'          => 'All Releases',
    'view_item'          => 'View Release',
    'search_items'       => 'Search Releases',
    'not_found'          => 'No release found',
    'not_found_in_trash' => 'No release found in Trash',
    'parent_item_colon'  => '',
    'menu_name'          => 'Release'
  );

  $args = array(
    'labels'             => $labels,
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,
    'query_var'          => true,
    'rewrite'            => array( 'slug' => 'release' ),
    'capability_type'    => 'post',
    'has_archive'        => true,
    'hierarchical'       => false,
    'menu_position'      => null,
    'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
  );

  register_post_type( 'release', $args );
}
add_action( 'init', 'codex_custom_init5' );

My Post Type

2

get_field is a replacement for get_post_meta you have to use only it:

$my_post_meta = get_field('album_image', 556);

if ( ! empty($my_post_meta) ) {
   // use the image
}

If you don't know the ID and want retrieve the post meta via post name, you have to use WP_Query or get_posts to ge the post via slug:

$posts = get_posts('post_name=here-the-post-name&post_type=release');
$mypost = ( ! empty($posts) ) ? array_pop($posts) : false;
$my_post_meta = $mypost ? get_field('album_image', $mypost->ID) : '';
if ( ! empty($my_post_meta) ) {
  // use the image
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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