In regards to your decision not to use WP_Query, pre_get_posts
is actually an excellent choice to make, rather than creating a new instance of WP_Query. In fact, pre_get_posts
is exactly what you should be using when you want to change the main query. Rather than executing a separate query on each page load, it modifies the main query’s parameters before it executes. Much more efficient!
You’re almost there. You just need to add !is_admin()
to your if
statement. You also should specifically call is_main_query()
on the $query
object passed to your function.
With these two changes your if
statement would now read:
if( !is_admin() && $query->is_main_query() && is_post_type_archive( 'properties' ) && !$_SESSION ) {
It’s also worth noting that you don’t need to return a value to a WordPress action – as opposed to a filter. With pre_get_posts
, the argument is passed by reference so any changes you make to your $query
variable will take effect automatically.