Retrieve WordPress posts array with ID and title
Tags: php, wordpress, webdev, tutorial
Published: 2020-05-04 07:21:50


<h3>Introduction</h3>

<p>As a WordPress developer, I often feel the need for post title and post ID together. It seems easy task using the available WordPress function like <code>get_the_title()</code>. 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?</p>

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

<h3>Where to write</h3>

<p>Any code snippet can be written directly in your theme's <strong>"functions.php"</strong> 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 <code>require()</code> function.</p>

<h3>Code snippet</h3>

<pre class="wp-block-code"><code>/**
 * 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&#91;$newPosts->ID] = esc_html($newPosts->post_title);
	}

	return $posts;

}</code></pre>

<h3>How to use</h3>

<p>As you may see, the function <code>tartist__posts_array()</code> 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 <code>var_dump( tartist__posts_array() );</code> it will show you the returned array just like the example below -</p>

<pre class="wp-block-code"><code>array (size=3)
  1 => string 'Title 1' (length=7)
  2 => string 'Title 2' (length=7)
  3 => string 'Title 3' (length=7)
 
  ... and so on</code></pre>

<p>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.</p>

<h3>A practical example of use</h3>

<p>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 -</p>

<pre class="wp-block-code"><code>$postsArray = tartist__posts_array();

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

<p>The above code will render a drop down select with options in HTML format like this -</p>

<pre class="wp-block-code"><code>&lt;select>		
	&lt;option value="1">Title 1&lt;/option>
	&lt;option value="2">Title 2&lt;/option>
	&lt;option value="3">Title 3&lt;/option>
&lt;/select></code></pre>

<p>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 <a rel="noreferrer noopener" href="https://github.com/amitbiswas06/tartist-elementor-extension" target="_blank">GitHub</a> page.</p>

<p>Let me know if you have more ideas on this function.</p>

<h3>Resources</h3>

<ol><li><a rel="noreferrer noopener" href="https://developer.wordpress.org/reference/functions/get_posts/" target="_blank">All about get_posts function.</a></li></ol>