10up / ElasticPress

A fast and flexible search and query engine for WordPress.
https://elasticpress.io
GNU General Public License v2.0
1.25k stars 311 forks source link

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

Closed rstrah closed 3 months ago

rstrah commented 4 months ago

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

felipeelia commented 4 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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

github-actions[bot] commented 3 months ago

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[bot] commented 3 months ago

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.