• Hi everyone,

    I have to exclude some articles based on a name of a person from the search results.

    I found the file ttps://core.trac.www.ads-software.com/browser/tags/4.4.2/src/wp-includes/query.php and I think I have to modify it in order to exclude posts from results.

    I do not took the time to read the entire file (Im’ a little bit lazy and also I have to find a solution in the next two days…) because perhaps someone has the name of a method to override to point to my vue (it will be great).

    Let me know if you need some more informations in order to answer my question and sorry if it seems that I did not search enough, I can do some more work by myself if needed.

    Any help would be very appreciated !

    Best Regards.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Can you not look at it backwards and exclude it from search?

    might worth a try or perhaps a look at its code:
    https://www.ads-software.com/plugins/search-exclude/

    good luck anyways

    Never edit core files.
    Search results page is managed by WP or you have a custom one?

    “based on a name of a person”

    is that name in the author field or in the title of the post?

    Thread Starter exaju

    (@exaju)

    Hi Alessio Lo Vecchio,

    Thank you for your answer. I need to exclude from search results all occurences of a name of a person that is displayed into several articles. It’s due to a legal issue. If the edition of core files is not recommended how to do in another way that type of hack ?

    Thanks db306 to you too. I look at this plugin and I think it can do the job.

    @alessio : could you give me the right way to do this type of hack ?

    Regards.

    Moderator bcworkz

    (@bcworkz)

    I think I can answer your question to Alessio if you don’t mind.

    We normally alter search queries with the ‘pre_get_posts’ action, but the WP_Query object passed doesn’t support exclusion search terms. About the only way to add exclusion terms would be by using the ‘posts_where’ filter. This is applied in the query.php file (line 3190), so you were on the right track, just going about it the wrong way.

    The filter passes the SQL WHERE clause to your callback. You would check for the signature of a search query, the presence of a couple LIKE operators. Return all other clauses unaltered. For search queries, prepend the necessary NOT LIKE statements linking to the other statements with AND. The trick is to be sure your addition is added inside the correct level of parenthesis.

    Use error_log() to output a typical search WHERE clause so you know what you’re working with. More on using filters is here: https://developer.www.ads-software.com/plugins/hooks/filters/

    Thread Starter exaju

    (@exaju)

    Hi bcworkz,

    Many thanks for this detailed explanation. I know now where to put this hack. But Alessio was saying to never hack core files, I think it is because these files are deleted by an update and changes are deleted also. No or there is another reason ? What would be a “good” way to implement this, make a plug-in ?

    Regards.

    Moderator bcworkz

    (@bcworkz)

    You’ve got it, that’s exactly the reason.

    Yes, a plugin is a good solution. Many people have a site specific plugin that contains all of their hacks and tweaks for that site.

    The other good solution is to create a child theme. This is a better approach it your hacks involve altering theme files. It’s best to avoid altering theme files for the same reason. Unfortunately, many commercial themes are installed as child themes because a common framework is installed as the parent. This takes away our ability to easily alter themes because a ‘grandchild’ theme is not really feasible.

    Thanks bcworkz for the right answers I didn’t get notifications for this thread. exaju do you have a lot of posts to exclude? Because if it’s not there is a very basic way to do this with the post__not_in parameter.

    For example you can find all the posts with the name of the person with strpos performed on the_content of all posts and when you get all the IDs you have the array to use, it’s just a one time operation.

    You will use that array in your default search page, without hacking anything. I know it’s not very smart and elegant, but could probably work…

    Thread Starter exaju

    (@exaju)

    Hi ijurisic87, bcworkz, and Alessio.

    I’ll see what is the best approach in order to to that type of hack.

    @ijurisic87 thanks, at this time I installed the first plugin in order to have similar feature to wait a plugin development to respond more precise to this require.

    @Allessio, I’ll see this implementation, anyway many thanks for the time and efforts to drive me to the right way ??

    @bcworkz, many thanks either for your detailed explanation. I must be more aware on how to make these childs theme and plugins which is the best way to to this (I tried chid theme once but there was issue with display; links to some CSS/PHP files was not good…)

    Anyway many thanks to you all to have spent time to explain me all of this, thats kind !!

    Sorry for my poor english ?? Regards to all of you !!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Remove articles from search result’ is closed to new replies.