Menü schliessen
Created: March 22nd 2021
Last updated: December 10th 2021
Categories: Php,  Wordpress
Author: Tim Fürer

WordPress: Get a Post

Tags:  guide,  PHP,  post,  wordpress
Donation Section: Background
Monero Badge: QR-Code
Monero Badge: Logo Icon Donate with Monero Badge: Logo Text
82uymVXLkvVbB4c4JpTd1tYm1yj1cKPKR2wqmw3XF8YXKTmY7JrTriP4pVwp2EJYBnCFdXhLq4zfFA6ic7VAWCFX5wfQbCC

In this quick guide, I'll show you how to get WordPress Posts.

To get posts, use this function:

get_posts();

Get WordPress Posts with Arguments

If you want to get posts by arguments, first, create an array:

$args = array(
	'posts_per_page'   => -1,
	'orderby'          => 'date',
	'order'            => 'DESC',
	'post_type'        => '{insert_post_type}',
	'post_status'      => 'publish'
);

The example shows some arguments you can use, be sure to set the values to what you need them to be. For a full list of arguments, check out the offical WordPress Documentation here (get_posts() specific) and here (WP_Query in general).

Now, pass on the arguments to the function like this:

get_posts($args);

Save Posts into Array and get each one

You might want to get each post you have retrieved, this is how you do it:

$retrieved_posts = get_posts($args);

foreach ($retrieved_posts as $key => $single_retrieved_post) {
	//this is a single post
	echo ($single_retrieved_post->post_content);
}

In this example, we even echo the post's content. If you want to do something else with it, just remove that line of code.