Support » Plugin: Related Posts by Taxonomy » Restricting Related Posts to Language

  • Is there a way to restrict the posts returned by the plugin to the current language being viewed? I’m getting duplicate posts, in each language, in what is returned.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author keesiemeijer

    (@keesiemeijer)

    Hi joelmcdwebworks

    How did you setup your multilingual site, with a plugin?

    WPML

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Try it with this in your (child) theme’s functions.php

    
    add_filter( 'related_posts_by_taxonomy_posts_clauses', 'rpbt_add_language_to_related_posts_query', 10, 4 );
    
    function rpbt_add_language_to_related_posts_query( $pieces, $post_id, $taxonomies, $args ) {
    	global $wpdb;
    
    	// Check if WPML is activated
    	if ( ! defined( 'ICL_LANGUAGE_CODE' ) ) {
    		return $pieces;
    	}
    
    	// Check if WPML database table exists.
    	$table_name = "{$wpdb->prefix}icl_translations";
    	if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) != $table_name ) {
    		return $pieces;
    	}
    
    	$pieces['join_sql'] .= " INNER JOIN {$wpdb->prefix}icl_translations ON ($wpdb->posts.ID = {$wpdb->prefix}icl_translations.element_id)";
    	$pieces['where_sql'] .= " AND {$wpdb->prefix}icl_translations.language_code = '" . ICL_LANGUAGE_CODE . "'";
    
    	return $pieces;
    }
    

    I think this should do it, but I didn’t test it properly with the WPML plugin activated.

    btw:
    consider creating a child theme instead of editing your theme directly – if you upgrade the theme all your modifications will be lost. Or create a plugin with the code above.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Restricting Related Posts to Language’ is closed to new replies.