• Resolved Nikita_Sp

    (@nikitasp)


    Hi!

    Thank you for this awesome plugin for blog promotion!
    But there is an issue I’ve faced – pushing translated posts to the related ones.

    The thing is normally you won’t face it because languages are not similar, but the Slavic group languages are. So that’s why I get the same post related but on other language.

    Maybe you could add some setting in the update to avoid it?
    It could be an option similar to “same author” or “similar post type”.
    It’s just another taxonomy at least and I hope it wont take a lot of time for you.

    Many thanks!
    I really appreciate that!

Viewing 13 replies - 16 through 28 (of 28 total)
  • Plugin Author Ajay

    (@ajay)

    @nikitasp

    I do think that the previous post does make sense to me. But, I’m thinking there is a mismatch in what I can actually do – In theory I should first create the array of posts that gets passed to the custom template, but this is done on the fly.

    I’ve been thinking this through. And, since you are anyway doing a custom install, why not just use the API and then use WP_Query if needed (probably in conjunction with crp_object_id_cur_lang

    Please see this:
    https://gist.github.com/ajaydsouza/968b24a052e858bf8926

    What it essentially does is pull out the posts using the plugin and then you have the freedom to manipulate it however you want – not sure if WP_Query would help you pull out / translate posts on the fly?

    Thread Starter Nikita_Sp

    (@nikitasp)

    @ajay

    In theory I should first create the array of posts that gets passed to the custom template, but this is done on the fly.

    Yeah, this is the mistake. It should not. The thing is your WPML compatibility code should be BEFORE custom template output. Not in the cycle foreach for the default template.

    I have NO custom install. I’m using just a custom template hook.

    I don’t need any freedom. It’s just fine with the custom template, why I need to use global queries, etc? I have the feeling that all the custom functions (like API or get_crp_posts_id) would return the same array as custom template function.

    I’ve reported about the plugin bug, I’ve researched what is causing it.
    You need just rewrite 2 lines (just move the WPML compatibility BEFORE custom template apply_filter).

    You’ll help a lot of developers, who using this awesome plugin. Please.
    Thanks.

    Plugin Author Ajay

    (@ajay)

    Are you able to send me a pull request in the GitHub repository where you have this fixed?

    I have no issues implementing this. It’s just that I just don’t use PolyLang (or WPML), nor have the time to experiment on this.

    Thread Starter Nikita_Sp

    (@nikitasp)

    @ajay I haven’t got the final code and doesn’t use github repos.
    It should take 30 mins maximum for you to fix this. It’s your plugin, isn’t it?

    Thanks.

    Plugin Author Ajay

    (@ajay)

    Yes it is, but this unfortunately doesn’t pay the bills, so I’ll work on it alongside other changes in my free time.

    I’ve filed a github issue. And, I think there might be a better way of doing this – potentially ensuring that the main function pulls the correct posts (already translated) rather than doing it here.

    Thread Starter Nikita_Sp

    (@nikitasp)

    @ajay ok, let’s say I would donate you some amount to improve the plugin.
    Does it make sense? What amount would be suitable for this fix?

    Plugin Author Ajay

    (@ajay)

    I had a go on a test install with PolyLang installed and one other language. Using this code below gave me the same results as with the filter disabled.

    
    function crp_template( $null, $results, $args ) {
    	global $post;
    	$html = '';
    	$results = array_slice( $results, 0, 3 );
    
    	$html .= '<ul class="custom_crp">';
    
    	$processed_results = array();
    
    	foreach( $results as $result ) {
    
    		$resultid = crp_object_id_cur_lang( $result->ID );
    
    		// If this is NULL or already processed ID or matches current post then skip processing this loop.
    		if ( ! $resultid || in_array( $resultid, $processed_results ) || intval( $resultid ) === intval( $post->ID ) ) {
    			continue;
    		}
    
    		// Push the current ID into the array to ensure we're not repeating it.
    		array_push( $processed_results, $resultid );
    
    		$result = get_post( $resultid );    // Let's get the Post using the ID.
    
    		$html .= sprintf( '<li><a href="%1$s">%2$s</a></li>', get_permalink( $result->ID ), get_the_title( $result->ID ) );
    	}
    	$html .= '</ul>';
    
    	return $html;
    }
    add_filter( 'crp_custom_template', 'crp_template', 10, 3 );
    
    
    Plugin Author Ajay

    (@ajay)

    @nikitasp

    And, in terms of serving a single set of results to the crp_custom_template, this piece of code can do the trick and you can simplify the code I posted above.

    I will incorporate that code into my plugin in v3.0.0 which is still a while away.

    
    function crp_template( $null, $results, $args ) {
    	global $post;
    	$html = '';
    	$results = array_slice( $results, 0, 3 );
    
    	$html .= '<ul class="custom_crp">';
    
    	foreach( $results as $result ) {
    
    		$html .= sprintf( '<li><a href="%1$s">%2$s</a></li>', get_permalink( $result->ID ), get_the_title( $result->ID ) );
    	}
    	$html .= '</ul>';
    
    	return $html;
    }
    add_filter( 'crp_custom_template', 'crp_template', 10, 3 );
    
    function support_polylang( $results, $args ) {
    	global $post;
    
    	$processed_ids     = array();
    	$processed_results = array();
    
    	foreach( $results as $result ) {
    
    		$resultid = crp_object_id_cur_lang( $result->ID );
    
    		// If this is NULL or already processed ID or matches current post then skip processing this loop.
    		if ( ! $resultid || in_array( $resultid, $processed_ids ) || intval( $resultid ) === intval( $post->ID ) ) { // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
    			continue;
    		}
    
    		// Push the current ID into the array to ensure we're not repeating it.
    		array_push( $processed_ids, $resultid );
    
    		$result = get_post( $resultid );    // Let's get the Post using the ID.
    		array_push( $processed_results, $result );
    	}
    	return $processed_results;
    }
    add_filter( 'get_crp_posts_id', 'support_polylang', 999, 2 );
    
    

    Finally, it isn’t the incentive of a donation that drives me to work on the plugin. A lot users have donated because the plugin helps them.

    I’ve got a day job that is higher priority. This plugin (and others) are done pretty much in my free time – something I’ve had extremely little of. So, the donation would help pay for my time, but it can’t prioritise this over my job.

    Thread Starter Nikita_Sp

    (@nikitasp)

    @ajay thank you. Seems the second hook is that I want for now (before you’ll add it as a part of the plugin in next update).

    First piece of code (in the previous post) is not good because we slice array BEFORE removing translations posts – so we can get total amount < 3.

    Ah, also I’ve just donated you some amount.
    Thanks.

    • This reply was modified 5 years, 3 months ago by Nikita_Sp.
    Plugin Author Ajay

    (@ajay)

    Thank you. Well received.

    The second part of code will be in the next version of the plugin. I need to dig a bit into its implementation for efficiencies but I like it so far.

    Thread Starter Nikita_Sp

    (@nikitasp)

    @ajay thanks, nice to hear it! Will wait for the update with info in changelog to remove additional script from theme functions.

    Thanks.

    Plugin Author Ajay

    (@ajay)

    Changelog will likely read something along the lines of “Updated compatibility with Polylang and WPML with correct language post selected at get_crp_posts_ids”

    I’ll fix the wording when it comes out. Will be in v3.0.0 so not before that.

    That is next on the list as soon as as I get Add to All out in the next few days.

    Plugin Author Ajay

    (@ajay)

    @nikitasp

    Just to let you know that I’ve implemented this in the Github repo.

    https://github.com/WebberZone/contextual-related-posts/issues/130

Viewing 13 replies - 16 through 28 (of 28 total)

The topic ‘Polylang compatibility’ is closed to new replies.