I need to add related posts to my custom post type (using current taxonomy term). I know how to display them, I just need to be able to retrieve the currently selected taxonomy terms.
Here’s what I’m doing:
$args = array(
'post_type' => 'mycustomposttype',
'posts_per_page' => 3,
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'thetaxonomy',
'terms' => 'theterm',
'field' => 'slug',
)
),
);
I need to dynamically populate “thetaxonomy” and “theterm” based on what’s currently selected on the page. Getting the term is easy enough, but I’m struggling to get “thetaxonomy” slug.
Thanks in advance!
FIGURED IT OUT!
In case anyone is looking to accomplish the same thing
$currentTax = get_the_taxonomies($post->ID);
foreach($currentTax as $slug => $tax) {
$currentSlug = $slug;
};
$theTerms = array();
$term_list = wp_get_post_terms($post->ID, $currentSlug, array("fields" => "all"));
foreach($term_list as $term_single) {
array_push($theTerms, $term_single->slug);
}
So the query would look like this…
$args = array(
'post_type' => 'mycustomposttype',
'posts_per_page' => 3,
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => $slug,
'terms' => $theTerms,
'field' => 'slug',
)
),
);