Home > OS >  Wordpress : Get posts but not the full WP_Post object
Wordpress : Get posts but not the full WP_Post object

Time:09-17

Does anyone know if there is a way in Wordpress's core functions to retrieve posts but not the full WP_Post object ? For example let's say only the post title and permalink ?

The full post object is very big and the standard queries quickly become slow as your site grows.

There is a return argument in the standard WP_Query : https://developer.wordpress.org/reference/classes/wp_query/#return-fields-parameter But this only provides few options and you can't really retrieve what you want.

Thanks for your help

CodePudding user response:

You can use the get_results() function. You can use simple SQL statements like SELECT x,y,z,... to get only the columns you need from a table of your choice.

Simple example:

function get_players() {
global $wpdb;
return $wpdb->get_results("SELECT a.id, a_name ROM player a");

}

CodePudding user response:

The docs say that only 3 options are supported, and none of them allow an ad-hoc selection of fields. To do something more custom, you'd probably need to use the wpdb class

  • Related