0

I need to query all of my posts as an array in example:

'options' => [
   'Post ID'  => __('Post Name'),
   'Post ID'  => __('Post Name'),
],

How can i do it?

tried this but it gives me only the ID's:

function query_posts_as_array(){
   $args = array(
      'post_status'   => 'publish',
      'fields'        => 'ids',
);
$result_query = new WP_Query( $args );

$ID_array = $result_query->posts;
wp_reset_postdata();
return $ID_array;
  • Just to clarify, are you looking to get an array back from your query, where each ID serves as the key, and the post name is the title? The opening to your question/post reflects what looks like an options array you might pass to the query or a php array to be used for a form select – Anson W Han Aug 26 '18 at 1:13
  • Exactly i need it as an options array, to be used for a form select in page builder, where i wann't to let user choose which posts to exclude from query. – Wojciech Wyszyński Aug 26 '18 at 1:18
  • 1
    You don't have to do a single API call to get what you want, it isn't always possible. Instead, try multiple steps, break it down into smaller steps – Tom J Nowell Aug 26 '18 at 3:57
1

You're trying to make WP_Query give you everything in a single API call, but that won't work.

Instead, break it down into multiple steps, e.g.:

  • Create an empty list
  • fetch all the posts
  • foreach post:
    • add its ID and name to the list
  • you now have alist of ID's and names
  • Thank you, i try to do this your way, which looks great but i am not sure how to add ID's and names to the list. – Wojciech Wyszyński Aug 26 '18 at 5:42
  • $id_list = []; foreach( $posts_from_query as $single) { $id_list[ $single->ID ] = $single->post_title; } @WojciechWyszyński – nmr Aug 26 '18 at 10:14
  • Thanks, i actually did this same way. The problem is, that everything work well in backend, but when i try to get post ID's to exclude from wordpress query arguments like this 'post__not_in' => array($excluded_posts),, it gives me only the first ID of the list. – Wojciech Wyszyński Aug 26 '18 at 14:28
  • @WojciechWyszyński excluding things in database queries is incredibly slow and expensive. Just exclude them manually in the loop in PHP, it's significantly faster – Tom J Nowell Aug 26 '18 at 17:42
0

I found that this code:

$args = array( 'posts_per_page' => -1, 'fields' => '', ); 
$posts_from_query = get_posts( $args ); 
$id_list = []; 

foreach( $posts_from_query as $single) { 
   $id_list[ $single->ID ] = $single->post_title; 
} 
return $id_list; 

prints this:

Array ( [0] => Nulla molestie venenatis odio nec pulvinar. [1] => Video Post ) 

It prints numbers from 0... instead of post ID's. What could i do wrong?

0

If you just need the post ID and post title, you can use a simple SQL query and avoid the multiple steps and php loops with the standard Wordpress query functions.

global $wpdb;
$option_list = $wpdb->get_results( "SELECT id, post_title FROM wp_6gdyzh_posts" );

This approach is a bit more efficient as it cuts down on the amount of querying/processing done by your webserver for this particular need.

However, with either this direct SQL query approach or the approach where you query all the posts and then loop through to get the title/build the array, you'll want to make sure you apply a limit to the main query if the posts/post-types you are querying are frequently being added to the site. If this does end up being the case, you may want to add a sort option and limit option to the query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.