1

I have number values on my most recent post that are placed via advanced custom fields. I want to be able to pull the data from a post into another page. this can be easily done via an ID: https://www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/ but what I cannot accomplish is having this pull from the most RECENT post. ACF support site make no mention of most recent.

     <?php
     $args = array( 'numberposts' => '1' );
      $recent_posts = wp_get_recent_posts( $args );
      foreach( $recent_posts as $recent ){
     // acf query in here. not included for brevity.
    endif; 
    }
     wp_reset_query();
     ?>
  • Since you're only getting the one most recent post, you probably don't need the foreach loop. What you need is the post ID, right? So what happens when you `var_dump($recent_posts);`? – git-e-up Aug 09 '19 at 22:20
  • 1
    oh, yeah, i think i see where you are going with this. i could pull the most recent post, get the id and set it as a variable, then use the variable when setting the acf query. I will try that a bit and report back. if that helps me solve my issue we can rephrase it and give you credit. either way, thanks, i have a new direction to try. – Preston Reyes Aug 09 '19 at 22:23

1 Answers1

2

From the Codex on https://developer.wordpress.org/reference/functions/wp_get_recent_posts/ (with slight modifications):

<?php
$recent_posts = wp_get_recent_posts(array(
    'numberposts' => 1, // Number of recent posts thumbnails to display
    'post_status' => 'publish' // Show only the published posts
));
foreach($recent_posts as $post) : ?>

        <a href="<?php echo get_permalink($post['ID']) ?>">
            <?php echo get_the_post_thumbnail($post['ID'], 'full'); ?>

            <p class="custom-class"><?php echo $post['post_title'] ?></p>
        </a>

       <?php
       $custom_field = get_field('custom_field_name', $post['ID']);//for ACF fields
       ?>

<?php endforeach; wp_reset_query(); ?>
Jarom
  • 570
  • 1
  • 8
  • 15