Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merging comments outside of single and comments_template #4

Closed
Lindfyrsten opened this issue Apr 19, 2024 · 5 comments
Closed

Merging comments outside of single and comments_template #4

Lindfyrsten opened this issue Apr 19, 2024 · 5 comments

Comments

@Lindfyrsten
Copy link

Trying to show comments for every post on archive using:

$comments = get_comments(array(
                'post_id' => get_the_ID(),
                'status' => 'approve'
));

This only gets me the current language comments. Is the plugin only supposed to work when on a single template?

@senlin
Copy link
Owner

senlin commented Apr 19, 2024

Hello, yes the plugin works for the single template which is where it gets the post ID of the other language(s).

@Lindfyrsten
Copy link
Author

Lindfyrsten commented Apr 19, 2024

Can I get the other ID's outside of single if I have one of the ID's?

In other words: Is it at all possible to merge outside of single?

@senlin
Copy link
Owner

senlin commented Apr 19, 2024

Can I get the other ID's outside of single if I have one of the ID's?

In theory yes.

I'm not sure if it is possible at all as it will get a bit more complicated. One can always try though.

@Lindfyrsten
Copy link
Author

Here is the solution if anyone is looking to do the same:

foreach ($languages as $code => $l) {
                if (!$l['active']) {
                    $otherID = apply_filters('wpml_object_id', $post_ID, $type, false, $l['language_code']);
                    if ($otherID) {
                        // Switch language
                        $sitepress->switch_lang($l['language_code']);
                        $post = get_post($otherID);
                        $othercomments = get_comments(array(
                            'post_id' => $otherID,
                            'status' => 'approve',
                            'order' => 'ASC'
                        ));
                        $comments = array_merge($comments, $othercomments);
                    }
                }
            }

Just like your plugin code, except with the addition of switch_lang.
Programatically switching language to the language code made it work.

Also if I may suggest adding if ($otherID) logic to multilingual-comments-wpml.php
I ran into an error where a post wasn't duplicated and it would show ALL comments from current language code because $otherID would return null.

Adding that check prevents that from happening:

	public function merge_comments($comments, $post_ID)
	{
		global $sitepress;

		remove_filter('comments_clauses', array($sitepress, 'comments_clauses'));

		$languages = apply_filters('wpml_active_languages', null, 'skip_missing=1');

		$post = get_post($post_ID);
		$type = $post->post_type;

		foreach ($languages as $code => $l) {
			if (!$l['active']) {
				$otherID = apply_filters('wpml_object_id', $post_ID, $type, false, $l['language_code']);
				if ($otherID) {
					$othercomments = get_comments(array('post_id' => $otherID, 'status' => 'approve', 'order' => 'ASC'));
					$comments = array_merge($comments, $othercomments);
				}
			}
		}

		if ($languages) {
			usort($comments, [$this, 'sort_merged_comments']);
		}

		add_filter('comments_clauses', array($sitepress, 'comments_clauses'), 10, 2);

		return $comments;
	}

@senlin
Copy link
Owner

senlin commented Apr 25, 2024

Thank you for the suggestion! I will add it to the source code (or if you want you can do a PR) sometime soon :)

senlin added a commit that referenced this issue May 22, 2024
* date: May 22, 2024
* add condition to prevent $otherID returning `null` ([issue #4](#4 (comment)) props to [@Lindfyrsten](https://github.com/Lindfyrsten)
* add Requires Plugins header introduced in WP 6.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants