wordpress get 10 posts of each custom post type

Solutions on MaxInterview for wordpress get 10 posts of each custom post type by the best coders in the world

showing results for - "wordpress get 10 posts of each custom post type"
Lya
24 May 2017
1function delete_front_page_query_results() {
2    delete_transient('post_data');
3
4    $query_cpt1 = array (
5            'posts_per_page'   => 3,
6            'post_type' => 'cpt1'
7            );
8    $query_cpt2 = array (
9            'posts_per_page'   => 3,
10            'post_type' => 'cpt2'
11            );
12    $query_cpt3 = array (
13            'posts_per_page'   => 3,
14            'post_type' => 'cpt3'
15            );
16    $query_cpt4 = array (
17            'posts_per_page'   => 3,
18            'post_type' => 'cpt4'
19            );
20    $query_cpt5 = array (
21            'posts_per_page'   => 3,
22            'post_type' => 'cpt5'
23            );
24
25    $query_results[] = get_posts($query_cpt1);
26    $query_results[] = get_posts($query_cpt2);
27    $query_results[] = get_posts($query_cpt3);
28    $query_results[] = get_posts($query_cpt4);
29    $query_results[] = get_posts($query_cpt5);
30
31    //flattening three dimentional array to two dimensonal array
32    $flatten_array =array();
33    foreach ($query_results as $data) {
34            foreach($data as $flatten_data) {
35                 $flatten_array[] = $flatten_data;
36            }
37    }
38
39    function cpt_array_sort($a, $b) {
40            return strtotime($b->post_date) - strtotime($a->post_date);
41    }
42    usort($flatten_array, 'cpt_array_sort');
43
44    //setting transient with the array
45    set_transient ( 'post_data', $flatten_array, 365*24*60*60);
46}
47add_action('publish_post', 'delete_front_page_query_results);
48