Title: Problem with the pre_get_posts action
Last modified: June 10, 2019

---

# Problem with the pre_get_posts action

 *  Resolved [tahtu](https://wordpress.org/support/users/tahtu/)
 * (@tahtu)
 * [7 years ago](https://wordpress.org/support/topic/problem-with-the-pre_get_posts-action/)
 * I’m using the pre_get_posts action to limit the search results further more. 
   Without Better Search, I can detect the search reqeusts inside of it with $query-
   >is_search(). This does not longer work after installing Better Search. Can you
   fix this issue?
 * Example:
    add_action( ‘pre_get_posts’, ‘my_pre_get_posts’ ); function my_pre_get_posts(
   $query ) { if ( $query->is_search() ) { … } }
 * I want to limit the search results to some categories only. Without Better search,
   I can do this with $query->set( ‘cat’, array(1, 2, 3) ). But after installing
   Better Search, this does not work further more. Right now, I don’t have any search
   result. Can you fix this issue too?

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Author [Ajay](https://wordpress.org/support/users/ajay/)
 * (@ajay)
 * [7 years ago](https://wordpress.org/support/topic/problem-with-the-pre_get_posts-action/#post-11625264)
 * Are you in a position to debug what the query that is generated when you’re doing
   this? You’ll need Query Monitor plugin for this.
 * I had to run some recent changes which is likely to cause this as the previous
   version had unnecessary additions to WHERE clauses which interfered with Better
   Search output.
 *  Thread Starter [tahtu](https://wordpress.org/support/users/tahtu/)
 * (@tahtu)
 * [7 years ago](https://wordpress.org/support/topic/problem-with-the-pre_get_posts-action/#post-11625404)
 * Today I installed Relevanssi to compare it with Better Search. Relevanssi is 
   much more complex and has a lot of additional features. But Better Search is 
   much smaller and let do MySQL the relevance calculation. Both solutions have 
   advantages.
 * So in the moment, it’s not easy for my to show the used SQL query to you, since
   I uninstalled Better Search for the moment.
 * But like I suggested to use the `WP_Query` class to you inside an other topic
   in this forum a few minutes before, I suggest to you: Do not investigate more
   time into your SQL generation at all. You waste a lot of time with this and you
   never can offer all the same features like `WP_Query` can do. Additional Webmaster
   like me should know the `WP_Query` implementation and its filtering with its `
   pre_get_posts` action.
 * Because of this, I will not support you with your own SQL generation, even I 
   have installed the Query Monitor and I know SQL good from previous jobs.
 * The disvantage of your own SQL gereration implementation forced me to analyse
   your PHP code to figure out, how I can implement my search restriction to some
   categories. After that, I had to find out, how WordPress implemented the category
   <-> post relationship and wrote more SQL code, than I could do this with the 
   well documented `WP_Query` class.
 * So with your own SQL generation, you not just wasted a lot of your time, you 
   wasted also a lot of my time. Because of this, I let you know so detailed my 
   knowledge about the `WP_Query` class to not to waste further time.
 * I hope you understood my suggestions, and why I don’t support you with your question.
 * I still don’t know, what is better: Better Search or Relevanssi. Since Better
   Search let do MySQL the job of calculating and apply it with, I believe Better
   Search has advantages againt Relevanssi too.
 * But maybe I wrong… 😉
 *  Plugin Author [Ajay](https://wordpress.org/support/users/ajay/)
 * (@ajay)
 * [7 years ago](https://wordpress.org/support/topic/problem-with-the-pre_get_posts-action/#post-11628423)
 * I’ve responded to the other post in the forum. I’ll close this for now and we
   can keep the thread going there.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Problem with the pre_get_posts action’ is closed to new replies.

 * ![](https://ps.w.org/better-search/assets/icon-256x256.png?rev=2988729)
 * [Better Search - Relevant search results for WordPress](https://wordpress.org/plugins/better-search/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/better-search/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/better-search/)
 * [Active Topics](https://wordpress.org/support/plugin/better-search/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/better-search/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/better-search/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [Ajay](https://wordpress.org/support/users/ajay/)
 * Last activity: [7 years ago](https://wordpress.org/support/topic/problem-with-the-pre_get_posts-action/#post-11628423)
 * Status: resolved