|
<?php |
|
|
|
/** |
|
* Returns a WP_Query of downloads that are in the same download category, |
|
* or download tag, order by latest, limited to only 3 |
|
* |
|
* @since 1.0.0 |
|
* |
|
*/ |
|
function my_edd_related_taxonomies_query(){ |
|
|
|
$cat_taxonomy = 'download_category'; |
|
$download_cat_obj = get_the_terms( $post->ID, $cat_taxonomy ); |
|
$download_cat_ids = array(); |
|
|
|
if ( $download_cat_obj && ! is_wp_error( $download_cat_obj ) ) { |
|
foreach( $download_cat_obj as $download_category_obj ){ |
|
$download_cat_ids[] = $download_category_obj->term_id; |
|
} |
|
} |
|
|
|
$tag_taxonomy = 'download_tag'; |
|
$download_tag_obj = get_the_terms( $post->ID, $tag_taxonomy ); |
|
$download_tag_ids = array(); |
|
|
|
if ( $download_tag_obj && ! is_wp_error( $download_tag_obj ) ) { |
|
foreach( $download_tag_obj as $download_category_obj ){ |
|
$download_tag_ids[] = $download_category_obj->term_id; |
|
} |
|
} |
|
|
|
global $post; |
|
|
|
$the_query = New WP_Query( array( |
|
'post_type' => 'download', |
|
'posts_per_page' => 3, |
|
'post_status' => 'publish', |
|
'post__not_in' => array( $post->ID ), |
|
'tax_query' => array( |
|
'relation' => 'OR', |
|
array( |
|
'taxonomy' => $cat_taxonomy, |
|
'field' => 'term_id', |
|
'terms' => $download_cat_ids |
|
), |
|
array( |
|
'taxonomy' => $tag_taxonomy, |
|
'field' => 'term_id', |
|
'terms' => $download_tag_ids |
|
) |
|
) |
|
) ); |
|
|
|
return $the_query; |
|
|
|
} |
|
|
|
|
|
// Usage, as in any "loop" |
|
$related_category_query = my_edd_related_taxonomies_query(); |
|
if ( $related_category_query->have_posts() ) : ?> |
|
<?php while ( $related_category_query->have_posts() ) : $related_category_query->the_post(); ?> |
|
<?php the_title(); ?> |
|
<?php endwhile; ?> |
|
<?php endif; ?> |