How to obtain the recent posts without their content in an efficient way?

How to obtain the recent posts without their content?

The same way you would any other post, just don’t use the post content.

Not fetching the post content will not improve performance, and may actually hurt performance. This is because WordPress would need to make an additional query if it needed the post content. When you query a post, WP primes temporary caches, this way it only requests a post once in a request, no matter how many times you ask for it.

By being more specific about which fields you want, you may think you’re improving performance, but you’re actually hurting it as WP can no longer cache, and has to generate bespoke couture results.

Asking only for specific fields is great for minimising bandwidth use between a server and a browser, you get a nice chunk of data with a footprint perfect for a mobile phone. But you are not a mobile phone, you’re a server in a data centre, and the database is probably on the same machine.

You want to be able to cache and bulk request data so that the query is cheap and reusable. This is especially powerful when combined with object caching.

It would be great to make a single DB request but I don’t know how to do that either.

This cannot be done, what you want to do requires multiple queries.

Even if you did find an API function to do it, that function would make 5 separate queries internally, no performance gains would be made.

other notes:

  • wp_get_recent_posts is a helper function. Cut it out and go directly to WP_Query
  • Posts are not arrays, they are meant to be objects of type WP_Post, so use $post->post_content instead
  • You would be better off storing either the post IDs or the post itself in $params['last_five_articles']
  • You should not modify post objects directly, it is bad practice and will cause issues and inconsistencies that lead to bugs.
    • For example, if you want to modify post titles, don’t change the posts title field, use a filter to modify the output instead