• Resolved Giuseppe Milo

    (@pixael)


    Hi
    i’m using the function get_crp_posts_id to get the similar post in a multilanguage site managed with WPML. Unfortunately the get_crp_posts_id function seems to ignore the language variable as it returns posts in different languages.
    Is there a way to fix it?
    Thanks.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Ajay

    (@ajay)

    This will need to have a rewrite of the function. Alternatively, you can modify the results once they are retrieved similar to how it is implemented in get_crp

    https://github.com/WebberZone/contextual-related-posts/blob/master/contextual-related-posts.php#L190-L196

    I’ll need to experiment with the latter

    Thread Starter Giuseppe Milo

    (@pixael)

    For the moment I’ve changed the query to select only posts from the current language:
    $sql = “SELECT DISTINCT $fields FROM $wpdb->posts inner join wp_icl_translations on wp_icl_translations.element_id = wp_posts.ID WHERE 1=1 AND wp_icl_translations.language_code='” . ICL_LANGUAGE_CODE . “‘ $where $groupby $having $orderby $limits”;

    Plugin Author Ajay

    (@ajay)

    You could do this a bit cleaner in that case by using a function to

    
    function same_language_crp( $where ) {
    
       $where .= " AND wp_icl_translations.language_code='" . ICL_LANGUAGE_CODE . "' ";
       return $where;
    
    }
    add__filter( 'crp_posts_where', 'same_language_crp' );
    
    Thread Starter Giuseppe Milo

    (@pixael)

    Yes I’ll do it and I’ll add a filter for the join as well ??

    lelebart

    (@lelebart)

    my 2cents on this:

    function same_language_crp_join( $join ) {
      global $wpdb;
    
      $join .= " INNER JOIN " . $wpdb->prefix . "icl_translations";
      $join .= " ON " . $wpdb->prefix . "icl_translations.element_id = " . $wpdb->posts . ".ID";
      return $join;
    }
    add_filter( 'crp_posts_join', 'same_language_crp_join' );
    
    function same_language_crp_where( $where ) {
      global $wpdb;
    
      $where .= " AND " . $wpdb->prefix . "icl_translations.language_code='" . ICL_LANGUAGE_CODE . "' ";
      return $where;
    }
    add_filter( 'crp_posts_where', 'same_language_crp_where' );
    Plugin Author Ajay

    (@ajay)

    Thanks. This definitely looks complete.

    @pixael does this work for you?

    Thread Starter Giuseppe Milo

    (@pixael)

    Sorry for not getting back to you @ajay!
    Yes, this is almost exactly what I did to get the same query I posted with the correct filters.

    Plugin Author Ajay

    (@ajay)

    That’s good to know.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘get_crp_posts_id ignores language’ is closed to new replies.