In the event that you need to get either the last or first post based on published date within your WordPress plugin or theme, you can use the get_boundary_post() function. In this article, we will show you how to use the get_boundary_post() function.

If you would like more information on creating WordPress plugins, see our tutorial series on creating your first WordPress plugin.

Basic usage

<?php get_boundary_post( $in_same_cat, $excluded_categories, $start ); ?>

The post information is placed within a WP_Post object in which you can then extract any post information you want from it.

Optional parameters

$in_same_cat: This optional parameter accepts a boolean (true or false) value in which it will determine if the post should be in the same category.

$excluded_categories: This optional parameter allows you to exclude category IDs that you do not want to be included within the posts that are returned.

$start: This optional boolean (true or false) value will allow you to define where it starts. For example, if you are getting the first post, this would be set to true. If getting the last post, you would set it to false.

Example

Displaying the title of the most recent post

In the following example, we use the get_boundary_post() function to get a WP_Post object and assign it to a variable. Then, we simply output the post title from the WP_Post object using the PHP echo command.

<?php
$boundary = get_boundary_post( false, '', false );
echo $boundary->post_title;
?>

References

For more information on the get_boundary_post() function, take a look at the get_boundary_post() WordPress Codex page.

Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address
Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

0 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!