skip to Main Content

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

3

Answers


  1. Chosen as BEST ANSWER

    The most appropriate solution I found to avoid big requests is to use the WPGraphQL plugin. It allows you to make GraphQL API calls on your Wordpress database, and directly specify in your request what you want in return. The server appears to be way less overloaded.

    In my case it is very appropriate, but if you want to stay in a classic PHP wordpress approach, the get_results() function is working too as said in the other answers.


  2. 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

    Login or Signup to reply.
  3. 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");
    

    }

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search