Created
August 4, 2018 06:26
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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