Skip to content

Commit 28d119e

Browse files
author
Marcel Moll
committed
Adding get_random_suggestion_by_tags.php
1 parent ef194a7 commit 28d119e

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+
$tags = get_the_terms( get_the_ID(), 'tag' );
3+
4+
//extract tag id's from term objects
5+
$tag_ids = array_column($tags, 'term_id');
6+
7+
//concat id's to a string with ',' as seperator
8+
$tags_string = implode($tag_ids, ',');
9+
10+
//get five random posts that are in any tag of the current post
11+
$posts = query_posts([
12+
'post_not_in' => get_the_ID(),
13+
'tag__in' => $categories_string,
14+
'order' => 'ASC',
15+
'orderby' => 'rand',
16+
'post_per_page' => 5
17+
]);

0 commit comments

Comments
 (0)