Skip to content

Commit 3a831b3

Browse files
author
Dale Nguyen
authored
Merge pull request #2 from mo2l/master
Create get_random_post-usggestion_by_categories.php
2 parents 7213b85 + ef194a7 commit 3a831b3

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//get categories of post
2+
$categories = get_the_terms( get_the_ID(), 'category' );
3+
4+
//extract category id's from term objects
5+
$cat_ids = array_column($categories, 'term_id');
6+
7+
//concat id's to a string with ',' as seperator
8+
$categories_string = implode($cat_ids, ',');
9+
10+
//get five random posts that are in any category of the current post
11+
$posts = query_posts([
12+
'post_not_in' => get_the_ID(),
13+
'cat' => $categories_string,
14+
'order' => 'ASC',
15+
'orderby' => 'rand',
16+
'post_per_page' => 5
17+
]);

0 commit comments

Comments
 (0)