Пока что быстренько набросал такой некрасивый вариант без учета влезания в соседние категории - нужно будет доделать потом:
$_product_id = get_the_ID();
$cat_ids = wp_get_object_terms( $_product_id, 'cats', array('fields'=>'ids') );
if ( is_array($cat_ids) and count($cat_ids) ) {
$_posts = get_posts( array(
'numberposts' => -1,
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'cats',
'field' => 'id',
'terms' => $cat_ids,
'include_children' => true,
)
),
) );
if ( is_array($_posts) and count($_posts) ) {
foreach ($_posts as $_post) {
$_index[] = $_post->ID;
$_posts_ids[$_post->ID] = $_post;
}
$post_index = array_search( $_product_id, $_index );
if ($post_index) {
$_products_ids = array();
$_index_count = count($_index);
if ( isset($_index[$post_index-2]) )
$_products_ids[] = $_index[$post_index-2];
elseif ($post_index-1 == 0)
$_products_ids[] = $_index[$_index_count-1];
else
$_products_ids[] = $_index[$_index_count-2];
if ( isset($_index[$post_index-1]) )
$_products_ids[] = $_index[$post_index-1];
else
$_products_ids[] = $_index[$_index_count-1];
if ( isset($_index[$post_index+1]) )
$_products_ids[] = $_index[$post_index+1];
else
$_products_ids[] = $_index[0];
if ( isset($_index[$post_index+2]) )
$_products_ids[] = $_index[$post_index+2];
elseif ($post_index == $_index_count-1)
$_products_ids[] = $_index[1];
else
$_products_ids[] = $_index[0];
if ( count($_products_ids) ) {
foreach ($_products_ids as $_id) {
if ( is_object($_posts_ids[$_id]) )
$_products[$_id] = $_posts_ids[$_id];
}
}
}
}
}