Trying to get post data from query in WordPress

Viewed 19

I'm trying to build a function that checks if a vendor's (post authors) membership status is active, if not then I don't want their products (post type) to be shown on the front end. I've not worked with the query in this way before and I'm close but currently, when I dump $q->posts from my foreach on one of the category pages from the front-end I'm getting an empty array.

How do I get the current queried pages post data in my foreach. Everything I find online says $q->posts should work

add_action( 'pre_get_posts', 'custom_sort_staff' );

function custom_sort_staff( $q ) {

    if ( $q->is_main_query() ) {

        if ( $q->query_vars['wc_query'] === 'product_query' ) {

            $inactive_products = array();

            foreach ( $q->posts as $i => $post ) {

                $post_id = $query->post->ID;
                $author_id = get_post_field( 'post_author', $post_id );

                $memberships = wc_memberships_get_user_active_memberships( $author_id );
                if ( empty( $memberships ) ) {
                    $inactive_products[] = $post->ID;
                }
            }

            if( count( $inactive_products ) > 0){
                $q->set( 'post__not_in', (array) $inactive_products );
            }
        }       
    }
}

Preview of what I see when I try debugging and dumping array

0 Answers
Related