Skip to content

Instantly share code, notes, and snippets.

@deniqwe
Last active March 30, 2017 12:13
Show Gist options
  • Select an option

  • Save deniqwe/ddddb0b61226ee2044643b9938a35209 to your computer and use it in GitHub Desktop.

Select an option

Save deniqwe/ddddb0b61226ee2044643b9938a35209 to your computer and use it in GitHub Desktop.
Wordpress: Select last posts from all categories && put all elements into single arrays
<?php
$categories = get_categories();
$do_not_duplicate = array();
$last_cat = array();
foreach ( $categories as $category ) {
if($category->term_id!==4){
$last_cat = array(
'cat' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => '1',
// 'orderby'=>'rand',
// 'category__not_in' =>4,
'post__not_in' => $do_not_duplicate
);
}//exclude
$query_cat = new WP_Query($last_cat);
if ( $query_cat->have_posts() ) {
while ( $query_cat->have_posts() ) {
$query_cat->the_post();
$do_not_duplicate[] = $post->ID;
$post_cat_thumbnail[] = get_the_post_thumbnail_url();
$post_cat_title[] = get_the_title();
$post_cat_excerpt[] = get_the_excerpt();
$post_cat_url[] = get_the_permalink();
}//while
}//if
wp_reset_postdata();
}//foreach
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment