Hi,
we are using Relevanssi as search filter as described here: https://toolset.com/forums/topic/filter-posts-by-content-from-multiple-lines-custom-field/
Now we need to display specific posts no matter what's the user is searching for. How can we manage this?
We tried to solve that by using two views but that's not useful as it mixes the alphabetical order.
I guess there must be a way to filter/display all posts which either contains the searched term or contain "display always" (or something like that).
There's not a simple way to do this from wp-admin. Let me see if there is a way we can set this up with some custom code, please stand by and I will give you some feedback shortly.
This slightly modified custom code uses the posts_where clause to add some posts to a query:
add_filter('wpv_filter_query', function($query, $settings, $view_id){
if($view_id == 123){
add_filter('posts_where', 'add_specific_posts', 10, 2);
}
return $query;
}, 10, 3);
function add_specific_posts($where, $query){
global $wpdb;
$where .= " OR $wpdb->posts.ID IN (456, 789)";
remove_filter('posts_where', 'add_specific_posts', 10);
return $where;
}
You can add this code in a child theme's functions.php file, or create a new code snippet in Toolset > Settings > Custom code. Please replace 123 with your post view's ID, and replace 456, 789 with a comma-separated list of the specific post IDs you wish to always include in the results.
Okay, sorry my code had a typo in it. I have corrected that in your custom code snippet and in my previous comment. I left the snippet active on the site, but I'm not sure how the search is supposed to work so I'm not clear if I'm seeing the correct results or not.
Hi Christian. Thank you. unfortunately it is not sending the correct results. If you go to hidden link you will see all listings. One of them is „Mothership“ (post id 577) This post should be visible all the time, no matter what the user puts in the search field. But when I search for example „E1 6GQ“ it displays all post which contains this term but hides post 577 (mothership) although it is defined in the snippet. What are we missing?
Many thanks!
Alex
Okay I see, it doesn't seem to be adding the correct post. It seems to have something to do with the other filters. Is it okay for me to make a clone of this site with the Duplicator plugin to present to my 2nd tier team? They can analyze the query and see if there's anything I can do with custom code to append the correct results.
Okay thanks, I'm working on my local copy. I've noticed if I change the Query Filter configurations to search Post Contents and Title, instead of also including fields (this setting is shown in qf.png), I can see Mothership appear in the results. Now that I understand this, I am able to reproduce that problem in a clean site installation. I set up a View of a custom post type and added Relevanssi and a text search field. I added similar custom code to always include a specific post in the results regardless of the search term. If I configure the query filter to only search title and contents, I can see the extra post added to the results. When I switch to search title, contents and fields, the extra post is not added. So I think I'm getting close to the root of the problem - when searching title, contents, and fields, the custom code is not applied as expected. I've asked my 2nd tier team to investigate this specific issue, since it definitely needs to be resolved to get your site to a point where we can continue debugging. Please stand by and I'll let you know what I find out.
Another team member took a look at the Relevanssi setup and recommended a change to the custom code:
add_filter( 'relevanssi_where','add_specific_posts1',10,2);
function add_specific_posts1($where){
global $wpdb;
global $WP_Views;
if($WP_Views->current_view == 347){
$where .= " OR relevanssi.doc IN (577)";
}
return $where;
}
I'm troubleshooting this change now, and I'll give you an update shortly. Thanks for your patience.