0

So, I need to echo the URL of a custom field from the last post published in a certain post type and with a particular taxonomy.

This code so far works OK, but even that numberposts = 1, $imagecurrent will contain the image URL from all the posts matching the conditions, not only the last one.

<?php 
    $args = array(
        'posts_per_page'=> 1,
        'post_type'     => 'exhibition',
        'tax_query' => array(
        array(
          'taxonomy' => 'exhibitions',
          'field' => 'id',
          'terms' => 720, // Where term_id of Term 1 is "1".
        )),
    );

    $the_query = new WP_Query( $args );

?>
<?php if( $the_query->have_posts() ): ?>
    <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <?php echo $imagecurrent = get_field('exhibition_menu_image');
    ?>
    <?php endwhile; ?>
<?php endif; ?>

<?php wp_reset_query(); ?>

What am I missing?

EDIT

I have included the code from the solution (posts_per_page), but it's still spitting out the URL from all posts, not only the last one.

3

numberposts doesn't exist for WP_Query instead use

"posts_per_page" => 1

| improve this answer | |
  • Still spitting more than one URL! : / – Eric Mitjans Jun 27 '16 at 11:51
  • That's true but note numberposts works for get_posts() and other wrapper functions. – Sumit Jun 27 '16 at 11:54
  • Can you update the code with latest one ? @EricMitjans – Sumit Jun 27 '16 at 11:56
  • @Sumit Done, but still not working. – Eric Mitjans Jun 27 '16 at 11:59
  • Then issue might be get_field() function. Try displaying post ID using the_ID() instead of get_field() and share how many IDs do you see ? – Sumit Jun 27 '16 at 12:16

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.