👍 Научим бесплатно правильно создавать сайты на WordPress. Подробнее →
Всем привет! Столкнулся с проблемой – как правильно вывести похожие записи из произвольной таксономии? Обычные посты из категорий выводятся без проблем, а вот с пользовательскими типами проблема. Вот этим кодом (ниже) мне удалось вывести записи, связанные одной таксономией, но в этом списке выводится, также, и текущий пост и как его скрыть я не могу понять. Просьба помочь или указать правильный путь =)
В functions.php
// Create a query for the custom taxonomy
function related_posts_by_taxonomy( $post_id, $taxonomy, $args=array() ) {
$query = new WP_Query();
$terms = wp_get_object_terms( $post_id, $taxonomy );
// Make sure we have terms from the current post
if ( count( $terms ) ) {
$post_ids = get_objects_in_term( $terms[0]->term_id, $taxonomy );
$post = get_post( $post_id );
$post_type = get_post_type( $post );
// Only search for the custom taxonomy on whichever post_type
// we AREN'T currently on
// This refers to the custom post_types you created so
// make sure they are spelled/capitalized correctly
if ( strcasecmp($post_type, 'product') == 0 ) {
$type = 'product';
} else {
$type = 'products';
}
$args = wp_parse_args( $args, array(
'post_type' => $type,
'post__in' => $post_ids,
'taxonomy' => $taxonomy,
'term' => $terms[0]->slug,
) );
$query = new WP_Query( $args );
}
// Return our results in query form
return $query;
}
в single.php
ID, 'products' );
while ( $related->have_posts() ): $related->the_post(); ?>
// дальше обычный цикл

