Yes, that is possible. All Taxonomies are stored in the db in the wp_term_taxonomy
table. The following are all listed as taxonomies
-
category
-
post_tag
-
link_category
-
post_format and
-
Custom taxonomies
All terms belonging to these taxonomies are stored in the wp_terms
table. Tags are terms of the post_tag
taxonomy, and all “categories” that you create in the post menu screen under “Categories” are actually terms of the taxonomy category
This makes it easy to combine the different taxonomies and terms in one query. To accomplish this, you are going to make use of the Taxonomy Parameters in WP_Query
to run a custom tax_query
OK, so first we need to get a list of all the categories and tags that belongs to a post. For categories you are going to use get_the_category
and for tags get_the_tags
. You are going to return that as an array which you are going to use in your custom query
global $post;
//get the categories a post belongs to
$cats = get_the_category($post->ID);
$cat_array = array();
foreach($cats as $key1 => $cat) {
$cat_array[$key1] = $cat->slug;
}
//get the categories a post belongs to
$tags = get_the_tags($post->ID);
$tag_array = array();
foreach($tags as $key2 => $tag) {
$tag_array[$key2] = $tag->slug;
}
Here is your custom query arguments. You can go and have look at the taxonomy parameters in the link to WP_query
I’ve supplied. Also go and look at all the parameters that you can use in WP_query
as some parameters from that tutorial have been depreciated a long time ago, for instance, like caller_get_posts
which was replaced with ignore_sticky_posts
$args = array(
'posts_per_page' => 5,
'orderby' => 'rand',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $cat_array,
'include_children' => false
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $tag_array,
)
)
);
$the_query = new WP_Query( $args );
You can go play around with the code and modify as you wish.