Related Posts in Kadence

Disclosure: This post may contain affiliate links – meaning I get a commission if you decide to make a purchase through my links, at no cost to you.

Kadence theme comes with a built-in Customizer setting for showing similar (related) posts on single posts. Let’s see how it actually works under the hood.

/kadence/template-parts/content/single-entry.php has:

<?php
if ( is_singular( get_post_type() ) ) {
	...
	if ( 'post' === get_post_type() && kadence()->option( 'post_related' ) ) {
		get_template_part( 'template-parts/content/entry_related', get_post_type() );
	}
	...
}

i.e., if the current page is a singular post and if Customizer > Blog Posts > Single Post Layout > Show Related Posts is enabled, then load template-parts/content/entry_related.php.

And this file has:

$bpc = new WP_Query( apply_filters( 'kadence_related_posts_carousel_args', $args ) );

Searching for kadence_related_posts_carousel_args in all the Kadence theme files leads us to inc/template-functions/single-functions.php which has:

/**
 * Get the related posts args.
 *
 * @param number $post_id the post id.
 * @return array query args.
 */
function get_related_posts_args( $post_id ) {
	if ( apply_filters( 'kadence_related_posts_use_tags', true ) ) {
		// Get categories.
		$categories = get_the_terms( $post_id, 'category' );
		if ( empty( $categories ) || is_wp_error( $categories ) ) {
			$categories = array();
		}
		$category_list = wp_list_pluck( $categories, 'slug' );
		// Get Tags.
		$tags     = get_the_terms( $post_id, 'post_tag' );
		if ( empty( $tags ) || is_wp_error( $tags ) ) {
			$tags = array();
		}
		$tag_list = wp_list_pluck( $tags, 'slug' );
		$related_args = array(
			'post_type'              => 'post',
			'posts_per_page'         => 6,
			'no_found_rows'          => true,
			'post_status'            => 'publish',
			// 'update_post_meta_cache' => false,
			// 'update_post_term_cache' => false,
			'post__not_in'           => array( $post_id ),
			'orderby'                => 'rand',
			'tax_query'              => array(
				'relation' => 'OR',
				array(
					'taxonomy' => 'category',
					'field'    => 'slug',
					'terms'    => $category_list,
				),
				array(
					'taxonomy' => 'post_tag',
					'field'    => 'slug',
					'terms'    => $tag_list,
				),
			),
		);
	} else {
		$categories = get_the_terms( $post_id, 'category' );
		if ( empty( $categories ) || is_wp_error( $categories ) ) {
			$categories = array();
		}
		$category_list = wp_list_pluck( $categories, 'term_id' );
		$related_args = array(
			'post_type'              => 'post',
			'posts_per_page'         => 6,
			'no_found_rows'          => true,
			'post_status'            => 'publish',
			// 'update_post_meta_cache' => false,
			// 'update_post_term_cache' => false,
			'post__not_in'           => array( $post_id ),
			'orderby'                => 'rand',
			'category__in'           => $category_list,

		);
	}
	return apply_filters( 'kadence_related_posts_args', $related_args );
}

Out of the box, the code in the if block runs since it returns true unless overridden using the provided filter hook.

What the code does here is to fetch the 6 latest posts having the same tag/tags or category/categories as the current post and show them in random order while excluding the current post.

If you want the related posts to be based only on categories, then add this snippet:

add_filter( 'kadence_related_posts_use_tags', __return_false() );

Similar Posts

3 Comments

  1. Hello,

    Great article, but I only want to display post for specific tag and no categories? Is this possible?

    add_filter( ‘kadence_related_posts_use_categories’, __return_false() );

    Regards,
    Robert-Jan Smid

  2. Hello,

    That’s awesome!

    But I wanted to display the related posts using the Kadence element. I’ve created a Kadence element that replace the single post style.

  3. I tried this snippet in my child themes functions file, but it throws an error on the related articles block.
    “`
    Uncaught TypeError: call_user_func_array(): Argument #1 ($callback) must be a valid callback, no array or string given…
    “`
    Maybe there’s been an update to Kadence that is throwing it off? Would this work?
    “`
    remove_filter( ‘kadence_related_posts_use_tags’, true );
    “`

Leave a Reply

Your email address will not be published. Required fields are marked *