Title: Run function if most recent post outside loop?
Last modified: August 19, 2016

---

# Run function if most recent post outside loop?

 *  [huntz](https://wordpress.org/support/users/huntz/)
 * (@huntz)
 * [17 years, 7 months ago](https://wordpress.org/support/topic/run-function-if-most-recent-post-outside-loop/)
 * Hello,
 * I have a function i run outside the loop on my single.php like so.
 * <?php echo display_message() ?>
 * I dont want to display the message if its the most recent post. Does anyone know
   how?
 * Thanks

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [huntz](https://wordpress.org/support/users/huntz/)
 * (@huntz)
 * [17 years, 7 months ago](https://wordpress.org/support/topic/run-function-if-most-recent-post-outside-loop/#post-900577)
 * Just to clarify the above is on my post page and the function is basically at
   the bottom of the page outside of the if (have_posts()) loop.
 * My understanding is i cant use **$posts[0] == $post** outside of the loop?
 * So would i use something like
    <?php $firstpost = get_posts( ‘numberposts=1&post_date&
   order=DESC’ );
 * if $firstpost = 1;
    echo display_message(); else; /* do nothing */ endif ?>
 *  Thread Starter [huntz](https://wordpress.org/support/users/huntz/)
 * (@huntz)
 * [17 years, 7 months ago](https://wordpress.org/support/topic/run-function-if-most-recent-post-outside-loop/#post-900583)
 * Or something like
 * $getlatestpost = get_posts(‘numberposts=1&order=DESC&orderby=post_date’);
    $latestpostid
   = $getlatestpost ->ID;
 * If $post->ID == $latestpostid;
    echo display_message(); else; /* do nothing */
   endif;
 * Would that work? Am I making any sense lol. I just want to run a function on 
   my single.php at the bottom of the page if its not on the latest post.
 *  [CookiesForDevo](https://wordpress.org/support/users/actionnerd/)
 * (@actionnerd)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/run-function-if-most-recent-post-outside-loop/#post-900978)
 * Here’s how I got it to work for me, let me know if you have any problems adapting
   it:
 *     ```
       <?php
   
       	// query database for most recent post in category 3
       	$query = get_posts('numberposts=1&category=3');
       	foreach ($query as $post) {
   
       	//set variable as title of most recent post
       	$title = get_the_title();
       	}
   
       	//start new loop
       	if ( have_posts() ) : while ( have_posts() ) : the_post();
   
       	//set variable as title of current article
       	$title2 = get_the_title();
   
       	//if titles are the same, do this
       	if ( $title == $title2 ) {
       ?>
       	<h1>In This Issue</h1>
       <?php
   
       	//else if in that category and titles are not the same, do this
       	} elseif ( in_category('3') && $title != $title2 ) {
       ?>
       	<h1>Previous Features</h1>
       <?php
   
       	//else if in different category, display standard message
       	} else {
       ?>
       	<h1><?php the_category(' '); ?></h1>
       <?php
       	}
       ?>
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Run function if most recent post outside loop?’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [CookiesForDevo](https://wordpress.org/support/users/actionnerd/)
 * Last activity: [16 years, 9 months ago](https://wordpress.org/support/topic/run-function-if-most-recent-post-outside-loop/#post-900978)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
