How to load WordPress posts using WP_Query

Spread the love

In order to use WordPress methods directly to pull posts or any other data, “wp-load.php” file from the active WordPress installation is required to be included in custom php.

Following code snippet shows how to load a posts details using WP_Query.

The above code is creating an arguments array to get the post by a specific post id.
If you want to pull the latest post, following arguments can be used.

The above code loads the post details based on the arguments passed. In this example we’re assuming that only one post has been loaded using the post id. Different properties of the post can be accessible using following WordPress methods: