5um17 / wp-extended-search

Extend default search to search in selected post meta, taxonomies, post types and all authors.
https://www.secretsofgeeks.com/2014/09/wordpress-search-tags-and-categories.html
GNU General Public License v3.0
20 stars 4 forks source link

pre_get_posts hook is overriding query #1

Closed abdullah1908 closed 4 years ago

abdullah1908 commented 4 years ago

pre_get_posts hook is overriding the wp_query for other plugins & showing default posts instead of the actual post type list.

Please make it specific so it wouldn't impact for other plugins.

You can try Simple Job Board to reproduce the bug.

5um17 commented 4 years ago

Hi,

pre_get_posts only runs on search query.

If it is affecting the search query then posts_search filter should be active too? Anyway could you please provide steps to reproduce the issue?

Robbertdk commented 4 years ago

I had the same kind of problem with the Facet WP plugin.

Facet WP creates custom search queries which differs from the main search query this plugin alters. But those FacetWP queries got also altered by this plugin and where resulting incorrect results.

I've added a pull request for this issue: https://github.com/5um17/wp-extended-search/pull/3

In my case I can implement it like this:

/**
  * Disable the WP Extended Search on facet filters queries
  *
  * @param Boolean $enabled
  * @param WP_Query $query
  *
  * @return Boolean
  */
function disable_wp_es_on_facet_queries($enabled, $query){
    if ( !empty($query->query['facetwp']) ) {
        return false;
    }
    return $enabled;
}
add_filter('wpes_pre_get_posts_enabled', 'disable_wp_es_on_facet_queries', 10, 2);
5um17 commented 4 years ago

Hi,

Thanks for reporting the issue!

Even if you disable the pre_get_posts still posts_search filter will be active. This will alter the results as well. I am not getting why only pre_get_posts causing issue?

Also, we have wpes_enabled filter which can remove both the filters in one go. This is not useful? Is it the secondary query?

Thanks

5um17 commented 4 years ago

Reproduced the issue with Simple Job board. The problem was $query->is_search is true this is fixed by checking for search string $query->get( 's' ).

Another issue was when a real custom search query is fired, there was no way to disable WPES based on query. The filter was working fine but still posts_search filter was active so instead of adding two filters I added check for query var disable_wpes if this set to true WPES's filters are inactive. Here #3

Example:-

add_action('parse_query', function ( $q ){
    $q->set('disable_wpes', true);
});

I hope this will work for you.

Robbertdk commented 4 years ago

Hi,

Good solution, this will work as well. Thanks!