Skip to content

Retrieve WordPress posts array with ID and title

  • Last updated: May 15, 2020
Retrieve WordPress posts array

Introduction

As a WordPress developer, I often feel the need for WordPress posts array with ID and title together. It seems easy task using the available WordPress function like get_the_title(). But outside the loop that only works if you know the post ID. However, what if we want to retrieve all posts title with ID? Or posts by category etc?

I will show you a powerful way to do that and store our data inside an array with $key=>$value settings and for this I will use the native WordPress function called “get_posts()“.


Where to write

Any code snippet can be written directly in your theme’s “functions.php” file or if you are writing a plugin, you may place it inside your main plugin file. Though if you have many snippets for your project, you may place the code inside a separate PHP file and then you can call the file inside the functions.php using the require() function.

Code snippet for posts array with ID and title together

/**
 * Retrieve posts
 * @param post_type
 * (string|array) A post type slug (string) or array of post type slugs. Default 'post'.
 * @param total_posts
 * (int) The number of posts to query for. Use -1 to request all posts. Default 10.
 * @param order
 * (string) Designates ascending or descending order of posts. 
 * Default 'DESC'. Accepts 'ASC', 'DESC'.
 * 
 * @return array( key => value )
 */
function tartist__posts_array( $post_type = '', $total_posts = '', $order = '' ){
	
	$get_all_posts = get_posts( array(
		'post_type'     => $post_type ? esc_attr( $post_type ) : 'post',
		'post_status'   => 'publish',
		'numberposts'   => $total_posts ? esc_attr( $total_posts ) : 10,
		'order'		=> $order ? esc_attr( $order ) : 'DESC'
	) );

	$posts = array();

	foreach( $get_all_posts as $newPosts ){
		$posts[$newPosts->ID] = esc_html($newPosts->post_title);
	}

	return $posts;

}

How to use

As you may see, the function tartist__posts_array() accepts 3 parameters and those are optional. If no parameters are given, this function will return the array with default settings. You can call this function in any WordPress template or function. For example, if you paste this code inside your single.php as var_dump( tartist__posts_array() ); it will show you the returned array just like the example below –

array (size=3)
  1 => string 'Title 1' (length=7)
  2 => string 'Title 2' (length=7)
  3 => string 'Title 3' (length=7)
 
  ... and so on

Now, you can easily use this array to extract the values and titles as per your need. You can also use this function to return a custom post type posts. The first parameter will handle that. The second parameter is for total number of posts you want to return and the last parameter is for ordering. I will show you a practical example now.

A practical example of use

Suppose if you need a dynamic drop down select for a form, you can easily do so by using the array like the example below –

$postsArray = tartist__posts_array();

<select>
<?php foreach( $postsArray as $key=>$value ){ ?>
	<option value="<?php echo esc_attr( $key ); ?>" ><?php echo esc_html( $value ); ?></option>
<?php } ?>
</select>

The above code will render a drop down select with options in HTML format like this –

<select>		
	<option value="1">Title 1</option>
	<option value="2">Title 2</option>
	<option value="3">Title 3</option>
</select>

Apart from this, there can be many more usages as you can imagine. It will save a lot of time for your project if you use it in proper place with proper ideas. I have used similar function in one of me free Elementor extension. You can download it from GitHub page.

Let me know if you have more ideas on this function.

Resources

  1. All about get_posts function.

Leave a Reply