1

I use get_posts ($arg); to query the post table. Usually I need only three fields: post_title, ID and post_excerpt. But the data returned are many more, including post_content that is usually huge.

I know I can use 'fields' => 'ids' to get only the ID. But I would like to use 'fields' => 'post_title' or 'fields' => 'post_excerpt'.

Is that possibile? If not possibile, which other method I could use?

1 Answer 1

4

get_posts() uses the WP_Query class to fetch posts.

Usually, it returns an array of post objects. The $fields parameter for WP_Query accepts two valid arguments: 'ids' will make it return an array of post IDs and 'id=>parent' will make it return an associative array with the post objects properties as key => value pairs. Any other input value as an argument for $fields will result in the objects being returned.

So either you gather the IDs only, or you will get $post_content (and all other not needed info) back as well, either as an array item or an object property.

Generally, this ought not to be a performance issue though. It might be noticeable, if the resulting array itself is very large as well and you want to use it in further operations.
If your DB is so large, that optimized queries are required, consider that it still has to go through the entire wp_posts table, whether post content is actually selected or not.
Bottom line: It shouldn't matter at all, that the objects have some properties you do not need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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