Closed rstrah closed 3 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?
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.";
}
}
}
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."?
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
@rstrah I'm still a bit unclear, sorry. Old posts do or do not show up when excluded from search?
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.
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.
Describe your question
Code of Conduct