Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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
}
share|improve this answer
    
Thank you so much for your help :) –  Rajnikanth Oct 15 '13 at 6:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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