WAIT: Pre_get_posts not working on archive

I’m using the below code to only show posts on my archive page that have a certain value in post meta. It works fine in the default wp template, however when i create a Bricks archive template with a query loop set to default, the pre_get_posts doesn’t work. (Meaning it shows all posts)

Am I doing something wrong?

function filter_note_archive( $query ) {
    if ( $query->is_main_query() && !is_admin() && is_post_type_archive( 'note' ) ) {
        $meta_query = array(
            array(
                'key'     => 'note_visibility', 
                'value'   => 'public',   
                'compare' => '=',       
            ),
        );
        $query->set( 'meta_query', $meta_query );
    }
}

add_action( 'pre_get_posts', 'filter_note_archive' );

Hey @avi,

thanks for your report.

I will try to reproduce that issue and get back to you as soon as I have any findings. In the meantime please try to use the bricks/posts/query_vars hook instead:

add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
    if ( $element_id === 'your_query_loop_element_id' ) {
        $query_vars['meta_key'] = 'note_visibility';
        $query_vars['meta_value'] = 'public';
    }

    return $query_vars;
}, 10, 3 );

In fact you actually don’t need any hook for what you’re trying to achieve. You can use the query builder in your archive template to add your meta query as the query parameters you specify get merged with the main query by default.

Let me know if that helps.

Best,

André

Thanks @aslotta I wasn’t aware that the query builder gets merged with the default query. But it would be nice if I can use pre_get_posts if I can.