Skip Navigation

[Résolu] display specific posts to filter search results no matter of searched term

This support ticket is created Il y a 4 années et 5 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Ce sujet contient 20 réponses, a 3 voix.

Dernière mise à jour par Shane Il y a 4 années et 5 mois.

Assisté par: Shane.

Auteur
Publications
#1662895

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).

Thanks for your support,
Alex

#1663675

Hi, are you trying to display specific posts in addition to the results returned by the text search, or instead of the results returned by the search?

#1663687

Hi Christian, in addition.

Thanks!
Alex

#1663829

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.

#1663987

Quick question - do you plan to use pagination to display the search results, or will the list of results always be shown on a single page?

#1664247

It is not planned yet. I guess display all results on a single page will be fine.
Thanks

#1666907

Okay thanks for your patience. Another supporter mentioned the proposed solution in this ticket, and I think it can be adapted to work for your case: https://toolset.com/forums/topic/create-a-view-that-pulls-in-all-posts-and-only-certain-pages-by-id/#post-1605249

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.

#1667881

Hi Christian,
thanks it looks promising but somehow it doesn't work. It displays all kind of posts/custom fields then.

#1668747

May I log in to the wp-admin area to debug this? Where can I see the View on your site?

#1672207

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.

#1672419

Hi Christian. Thank you. unfortunately it is not sending the correct results. If you go to lien caché 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

#1673519

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.

#1673565

Yes for sure!
Thank you!
Alex

#1674071
qf.png

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.

#1676523

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.