Adding "fields" to arguments array for get_posts()/query_posts()/WP_Query()
|Reported by:||mikeschinkel||Owned by:|
I find myself more and more often needing to get a list of post IDs so I can call another WordPress database API function to include or exclude those posts. Calling get_posts() on a potentially large number of records and passing all the fields (especially post_content) between MySQL and WordPress is hugely inefficient when I only need the one ID.
My two (2) options are:
1.) Code is directly in SQL. This is easy but I know it's definitely not a best practice and I would like to use the WordPress API wherever possible.
2.) To use a post_fields hook. Problem is that those are global and I have to start wrapping logic around my code to ensure I don't accidentally break some plugin or some other part of WordPress (this approach is much like trying to secure a server by starting with all the attack vectors open and then trying to close them all.)
So I'd like to propose we simply add "fields" as a recognized argument for get_posts(), i.e.
$posts = get_posts(array( 'fields' => 'ID,post_title', 'post_type' => 'movie', 'post_status' => 'publish', 'order' => 'ASC', 'posts_per_page' => -1 ));
I know I could make the same argument for joins, where, orderbys et. al. but I'd argue this is enough of a special case it could really use some early attention. For a query on the home page of a high traffic site that changes often enough to not be able to be cached this tiny change could make a major difference in performance.
If you'd like to see a use case here is one:
Unfortunately I still struggle with creating patches even though I have done it a few times in the past but each time I seem to have to start relearning from scratch. I seem to have a mental block for some reason on patches (''can someone help me with this?'') so I haven't gone ahead and written a patch but will tackle it if I get the task blessed.
Change History (20)
- Keywords needs-patch added
- Milestone changed from Awaiting Review to 3.1
comment:17 scribu — 3 years ago
- Keywords needs-patch 3.2-early added; has-patch removed
- Milestone changed from 3.1 to Future Release
comment:18 scribu — 3 years ago
- Keywords needs-patch 3.2-early removed
- Milestone changed from Future Release to 3.1
- Resolution set to fixed
- Status changed from new to closed