I am working on a WprdPress/WooCommerce plugin feature where specific Customers are shown only a subset of Products.
The user has a meta data field with an array of post ids corresponding to the allowed Products.
I am trying to hook into the main query:
add_action( 'pre_get_posts', 'customprefix_pre_get_posts' );
function customprefix_pre_get_posts( $query )
{
$user = wp_get_current_user();
if ( $query->is_main_query() && is_product_category() )
{
/** @var array $allowed Post IDs */
$allowed = customprefix_get_allowed_products_per_user( $user );
$query->set( 'post__in', $allowed );
}
}
I have tried many different combinations of how to add the post__in
clause to the query including $query->query_vars['post__in'] = $allowed
and others. I have tried adding $query->parse_query_vars()
as well with no success.
While diving further into the get_posts()
function in WP_Query
it appears that my change to the Query is returned correctly (by reference) from the pre_get_posts
hook. The next line: $q = $this->fill_query_vars( $q );
somehow looses my custom post__in
field.
Most of the documentation for WP_Query
and post__in
revolve around creating a new query, not modifying the main query.