Skip to content

Instantly share code, notes, and snippets.

@mdjwel
Created August 4, 2018 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdjwel/c041dbfd9b47496e48470b9b78aecce1 to your computer and use it in GitHub Desktop.
Save mdjwel/c041dbfd9b47496e48470b9b78aecce1 to your computer and use it in GitHub Desktop.
Get post all post titles of a post type in a associative array of post_title => ID pair in WordPress
<?php
// query for your post type
$products = new WP_Query(
array (
'post_type' => 'product',
'posts_per_page' => -1
)
);
// we need the array of posts
$posts_array = $products->posts;
// create a list with needed information
// the key equals the ID, the value is the post_title
$post_title_array = wp_list_pluck( $posts_array, 'post_title', 'ID' );
// See the output of the array
echo '<pre>'.print_r($post_title_array, 1).'</pre>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment