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

How to exclude news from a related post if the field ep_exclude_from_search.value is true? #3926

Closed
1 task done
rstrah opened this issue Jun 5, 2024 · 7 comments
Closed
1 task done

Comments

@rstrah
Copy link

rstrah commented Jun 5, 2024

Describe your question

add_filter(
    "ep_formatted_args",
    function ($formatted_args, $args) {
        if (empty($args["more_like"])) {
            return $formatted_args;
        }

        if (!@isset($formatted_args["query"]["more_like_this"])) {
            return $formatted_args;
        }

       
        $formatted_args["query"]["bool"]["must_not"][] = [
            "term" => [
                "meta.ep_exclude_from_search.value" => "1"
            ]
        ];

        $formatted_args["query"]["more_like_this"]["max_query_terms"] = 30;

        return $formatted_args;
    },
    20,
    2
);

Code of Conduct

  • I agree to follow this project's Code of Conduct
@felipeelia
Copy link
Member

felipeelia commented Jun 7, 2024

Instead of using the ep_formatted_args filter, @rstrah, can you please try to add add_filter( 'ep_bypass_exclusion_from_search', '__return_false' ); just before calling WP_Query or one of the Related Posts API functions?

@rstrah
Copy link
Author

rstrah commented Jun 10, 2024

Great, it works
Update
It works half-heartedly, in old articles everything is still covered in the old way.

<?php
// More https://gist.github.com/felipeelia/b026601c54c71c1b81c98c54088c03f2
function ep_get_related_posts($post_id = null, $related_post_count = 3)
{
    if (class_exists("\ElasticPress\Features")) {
		add_filter( 'ep_bypass_exclusion_from_search', '__return_false' );
        add_filter("ep_find_related_args", function ($args) {
            $args["post_type"] = ["news"];
            return $args;
        });

        add_filter(
            "ep_formatted_args",
            function ($formatted_args, $args) {
                if (empty($args["more_like"])) {
                    return $formatted_args;
                }

                if (!@isset($formatted_args["query"]["more_like_this"])) {
                    return $formatted_args;
                }

                $formatted_args["query"]["more_like_this"][
                    "max_query_terms"
                ] = 30;
                return $formatted_args;
            },
            20,
            2
        );

        $related_posts = \ElasticPress\Features::factory()
            ->get_registered_feature("related_posts")
            ->find_related($post_id, $related_post_count);

        if ($related_posts) {
            echo "<h3>Читайте также:</h3>";
            echo "<ul>";
            foreach ($related_posts as $related_post) {
                echo '<li><i><b><a href="' .
                    get_permalink($related_post->ID) .
                    '">' .
                    $related_post->post_title .
                    "</b></i></a></li>";
            }
            echo "</ul>";
        } else 
		{
			$user = wp_get_current_user();
			$allowed_roles = array('administrator');
			if (array_intersect($allowed_roles, $user->roles)) {
			echo "No related posts found.";	
			}
		}
    } else {
		if (array_intersect($allowed_roles, $user->roles)) 
		{
        echo "ElasticPress is not available.";
		}
    }
}

@felipeelia
Copy link
Member

Hi @rstrah! Sorry, I'm not sure I follow. What do you mean by "in old articles everything is still covered in the old way."?

@rstrah
Copy link
Author

rstrah commented Jun 19, 2024

Hi @rstrah! Sorry, I'm not sure I follow. What do you mean by "in old articles everything is still covered in the old way."?

In old posts (published a long time ago) the problem still remains. But in general the problem is solved, thanks for the help

@felipeelia
Copy link
Member

@rstrah I'm still a bit unclear, sorry. Old posts do or do not show up when excluded from search?

Copy link

It has been 3 days since more information was requested from you in this issue and we have not heard back. This issue is now marked as stale and will be closed in 3 days, but if you have more information to add then please comment and the issue will stay open.

@github-actions github-actions bot added the stale label Jun 24, 2024
Copy link

This issue has been automatically closed because there has been no response to our request for more information in the past 3 days. With only the information that is currently available, we are unable to take further action on this ticket. Please reach out if you have found or find the answer we need so that we can investigate further. When the information is ready, you can re-open this ticket to share it with us.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants