Retrieving 3 latest post from each of 5 different custom post types

I am not sure, if this is the most efficient option or not, but I have doen it in following way:

  1. I have stored 5 separate query using get_posts and stored them in a single array
  2. I had to flatten the array, since it was multidimensional. Then used usort using post_date to get the latest post
  3. I have saved this sorted data using Transient API, to minimize database call.

Here is the code:

function delete_front_page_query_results() {
    delete_transient('post_data');

    $query_cpt1 = array (
            'posts_per_page'   => 3,
            'post_type' => 'cpt1'
            );
    $query_cpt2 = array (
            'posts_per_page'   => 3,
            'post_type' => 'cpt2'
            );
    $query_cpt3 = array (
            'posts_per_page'   => 3,
            'post_type' => 'cpt3'
            );
    $query_cpt4 = array (
            'posts_per_page'   => 3,
            'post_type' => 'cpt4'
            );
    $query_cpt5 = array (
            'posts_per_page'   => 3,
            'post_type' => 'cpt5'
            );

    $query_results[] = get_posts($query_cpt1);
    $query_results[] = get_posts($query_cpt2);
    $query_results[] = get_posts($query_cpt3);
    $query_results[] = get_posts($query_cpt4);
    $query_results[] = get_posts($query_cpt5);

    //flattening three dimentional array to two dimensonal array
    $flatten_array =array();
    foreach ($query_results as $data) {
            foreach($data as $flatten_data) {
                 $flatten_array[] = $flatten_data;
            }
    }

    function cpt_array_sort($a, $b) {
            return strtotime($b->post_date) - strtotime($a->post_date);
    }
    usort($flatten_array, 'cpt_array_sort');

    //setting transient with the array
    set_transient ( 'post_data', $flatten_array, 365*24*60*60);
}
add_action('publish_post', 'delete_front_page_query_results);

Leave a Comment