Support » Theme: Kadence » Related Posts

  • Resolved digitalsankalp

    (@digitalsankalp)


    I want to show related posts that have the same tags as that of the current post. No the same category, only same tags.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi,
    There isn’t an option to ignore categories in the related posts query, you would have to use PHP to filter the query args to create your own without categories. The filter is “kadence_related_posts_args” and you can use that to return a custom array of query args.

    I hope that helps

    Ben

    Thread Starter digitalsankalp

    (@digitalsankalp)

    Can you help me with the code because I am not a developer.😊

    Hey,

    I can’t suggest you do this if your not comfortable with the code. This isn’t something I would recommend regardless, categories are a form of defining what is related.

    However, I can point you in the right direction as long as you understand this is not a “support” thing, you would need to hire a developer if you need custom work or custom code.

    https://kadence-theme.com/knowledge-base/advanced/how-to-add-a-custom-filter-or-function-with-code-snippets/

    add_filter('kadence_related_posts_args', 'custom_related_args' );
    function custom_related_args( $args ) {
            $post_id = get_the_ID();
            $tags     = get_the_terms( $post_id, 'post_tag' );
    	if ( empty( $tags ) || is_wp_error( $tags ) ) {
    		$tags = array();
    	}
    	$tag_list = wp_list_pluck( $tags, 'slug' );
    	$args = array(
    		'post_type'              => 'post',
    		'posts_per_page'         => 6,
    		'no_found_rows'          => true,
    		'post_status'            => 'publish',
    		'post__not_in'           => array( $post_id ),
    		'orderby'                => 'rand',
    		'tax_query'              => array(
    			array(
    				'taxonomy' => 'post_tag',
    				'field'    => 'slug',
    				'terms'    => $tag_list,
    			),
    		),
    	);
            return $args;
    }
    Thread Starter digitalsankalp

    (@digitalsankalp)

    It worked. Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.