Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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