cyberhobo / wordpress-geo-mashup

Official repository for Geo Mashup, the plugin that makes WordPress into a GeoCMS. Documentation:
https://github.com/cyberhobo/wordpress-geo-mashup/wiki/Getting-Started
63 stars 15 forks source link

Is it possible to $query->set( 'geo_mashup_query', $geo_mashup_query ) on pre_get_posts action for the main query? #845

Closed pshemek closed 4 years ago

pshemek commented 4 years ago

Hello!

Thank you for your work on this powerful and flexible plugin.

I have the following problem.

When I create a custom query, geo location component works well:

$q = new WP_Query( [
  'post_type' => 'article',
  'geo_mashup_query' => [ 'country_code' => 'US' ]
] );

However, I need to use geo_mashup_query in the main query of archive page. Normally, I would use pre_get_posts hook for this purpose but when I'm trying:

add_action( 'pre_get_posts', 'custom_filters_query' );
function custom_filters_query( $query ) {
  if ( ! is_admin() && $query->is_main_query() && $query->is_post_type_archive( 'article' ) ) :
    $geo_mashup_query = [
      'country_code' => 'US',
    ];
    $query->set( 'geo_mashup_query', $geo_mashup_query );
  endif;
}

the results on archive page are not filtered.

What can be wrong? Is it possible to join the main query with geo query?

cyberhobo commented 4 years ago

I think you'll have to use the parse_query action instead of pre_get_posts and set the priority to 9 so your hook runs before Geo Mashup's.