2

I am using Advanced Custom Fields with Wordpress. I have a custom post type called VIDEOS which has two fields - video_link and video_artist.

I can call and output the video_link field, but I cannot seem to display the 'video_artist' field using the code below...

<?php 
   $posts = get_posts(array(
   'post_type'          => 'videos',
   'posts_per_page'     => -1
   )
   ));                  
   if( $posts ): ?>
<?php foreach( $posts as $post ): 
   setup_postdata( $post )                  
   ?>
<?php echo wp_oembed_get( get_field( 'video_link' ) ); ?>
<?php the_title(); ?>
<?php the_field('video_artist'); ?>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>

In fact, this line...

<?php the_field('video_artist'); ?>

...breaks the site and displays nothing at all after it appears. No html of any kind.

9
  • 1
    Try get_post_meta(): <?php echo get_post_meta($post->ID, 'video_artist', true); ?>. Commented Apr 19, 2016 at 19:50
  • Have you tried var_dump on the $posts variable? Also, if you go into your database, you may want to double check the actual meta value (that would be the best way to fully confirm that the correct field name is video_artist. The way I would do it is get your post's ID, go into MySQL (or phpMyAdmin) and try "SELECT * FROM wp_postmeta WHERE post_id = '{YOUR_ID}'" and it will output all of the meta fields for that post. You can 100% confirm if you should be seeing a value and if the meta key is correct. That should get you started. Commented Apr 19, 2016 at 19:52
  • Just did a var-dump and no mention of video_artist field at all. @MillerMedia Commented Apr 19, 2016 at 19:58
  • Maybe there is no custom field with name video_artist. Add simple check: <?php if (get_field('video_artist')) the_field('video_artist'); ?>. Here is details. Commented Apr 19, 2016 at 20:14
  • a check just prints out the word 'array' for every post. Commented Apr 19, 2016 at 20:16

2 Answers 2

2

It's more or less the same code as your, just tested, and it works fine

As @admcfadn said, your are note in a wordpress loop, so you need to add the post id as a parameter of the_field

$posts = get_posts(array(
    'post_type'          => 'videos',
    'posts_per_page'     => -1
));       


if( $posts ): 

    foreach( $posts as $post ): 
        setup_postdata( $post );               


        the_title(); 

        the_field('video_link', $post->ID);

        the_field('video_artist', $post->ID);

     endforeach;
     wp_reset_postdata();

endif; 

If you like to use the loop without arg in the_field that will look like that:

$options = array(
    'post_type'          => 'videos',
    'posts_per_page'     => -1
);

$query = new WP_Query( $options );

if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); 

    the_title(); 

    the_field('video_link');

    the_field('video_artist');

endwhile; endif;

ps: you don't need to use <?php ?> on each line

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for helping. So your code works BUT it only shows the most recent post. How do I make it show every post? If I have 200 posts I want it to show all 200, not just 1.
Actually, no code at all is rendered on the page immediately after the first value. so the entire site, footer/rest of page are just blank in the code now.
The 2 versions with post_per_page => -1 will display all your video post. You can set this value to 200 to limit it at 200.
It should have an issue in your code, but without more information, i can't help you
0
$post->the_field('video_artist');

you're using get_posts, not wp_query, so you might need to refer to the variables via $post.

&/or troubleshoot it with the following:

the_field('video_artist', $post->ID); 

might get if for you.

Or...

$baz = get_field( 'video_artist' ); echo $baz;

Also, looks like you're missing a semi-colon after setup_postdata( $post ) and have an extra closing parentheses after get_posts

7 Comments

the_field('video_artist', $post->ID); should get if for you.
or... $baz = get_field( 'video_artist' ); echo $baz; ?
you're also missing a semi-colon after setup_postdata( $post )
this too just outputs the word 'array' instead of the field content for each post.
you can use print_r() or var_dump() on an array to find out what's inside. This one should work: the_field('video_artist', $post->ID); Are you using a repeater-field?
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.